abcde a3 aaaaaa aa #
0 3
#include <stdio.h> #include <string.h> #define SIZE 1100 char t[SIZE] , des[SIZE] ; int next[SIZE] ; void getNext() { int len = strlen(t) ; int i = -1 , j = 0 ; next[0] = -1 ; while(j<len) { if(i == -1 || t[i] == t[j]) { ++i,++j; next[j] = i ; } else { i = next[i] ; } } } int kmp() { int i = 0 , j = 0 ; int len1 = strlen(t) , len2 = strlen(des) ; int c = 0 ; getNext() ; while(j<len2) { if(i == -1 || t[i] == des[j]) ++i ,++j; else i = next[i] ; if(i == len1) { i = 0 ; c++ ; } } return c ; } int main() { while(~scanf("%s",des) && strcmp(des,"#")!=0) { scanf("%s",t) ; int ans = kmp() ; printf("%d\n",ans) ; } return 0 ; }
原文地址:http://blog.csdn.net/lionel_d/article/details/45195475