标签:print for efi turn space define mat strlen length
#include <stdio.h> #include <iostream> #include <math.h> #include <string.h> #define d 1e-7 using namespace std; int next[5],match[5005]; char text[5005]; char pattern[5]="NBA"; char take_place[5]="CBA"; inline void BuildNext(int length) { int i, t; i = 1; t = 0; next[1] = 0; while(i < length + 1) { while(t > 0 && pattern[i - 1] != pattern[t - 1]) { t = next[t]; } ++t; ++i; if(pattern[i - 1] == pattern[t - 1]) { next[i] = next[t]; } else { next[i] = t; } } //pattern末尾的结束符控制,用于寻找目标字符串中的所有匹配结果用 while(t > 0 && pattern[i - 1] != pattern[t - 1]) { t = next[t]; } ++t; ++i; next[i] = t; } int KMP(int text_length,int pattern_length) { int i, j, n,k; BuildNext(pattern_length); i = 0; j = 1; n = 0; while(pattern_length+1-j<= text_length - i) { if(text[i] == pattern[j - 1]) { ++i; ++j; //发现匹配结果,将匹配子串的位置,加入结果 if(j == pattern_length + 1) { match[n++] = i - pattern_length; j = next[j]; } } else { j = next[j]; if(j == 0) { ++i; ++j; } } } return n; } int main() { gest(text); int len_text = strlen(text); int len_pattern = strlen(pattern); int n = KMP(len_text,len_pattern); int i,j,k; //printf("%d\n",n); // for(i=0;i<n;i++) // cout<<match[i]<<" "; //cout<<endl; for(i=0;i<n;i++) { k = 0; for(j=match[i];j<match[i]+3;j++,k++) text[j] = take_place[k]; } printf("%s",text); return 0; }
标签:print for efi turn space define mat strlen length
原文地址:http://www.cnblogs.com/fzuhyj/p/8016257.html