标签:
#include<cstdio> #include<cstring> #include<cstdlib> void GetNext(char *t,int *next){ int i =1,j = 0; next[0] = 0; while(i<t[0]){ if(j == 0 || t[i] == t[j]){ i++; j++; if(t[i]!=t[j]){ next[i] = j; }else{ next[i] = next[j]; } }else{ j = next[j]; } } } int index_kmp(char *s,char *t,int pos){ int i = pos; int j = 1; int next[88]; GetNext(t,next); while(i < s[0] && j<=t[0]){ if(0 == j||s[i] == t[j]){ i++; j++; }else{ j = next[j]; } } if(j>t[0]){ return i-t[0]; }else{ return 0; } } int main(){ char t[88] = " aaaas"; char s[88]; t[0] = strlen(t)-1; while(~scanf("%s",s+1)){ s[0] = strlen(s); printf(" %d\n",index_kmp(s,t,1)); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zhuozhuo/p/5406258.html