标签:
http://acm.hdu.edu.cn/showproblem.php?pid=2087
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11793 Accepted Submission(s): 7574
#include<stdio.h> #include<string.h> #define N 1005 char a[N], b[N]; int Next[N], sum; void FindNext(int B) { int i=0, j=-1; Next[0] = -1; while(i<B) { if(j==-1 || b[i]==b[j]) Next[++i] = ++j; else j = Next[j]; } } void KMP(int A, int B) { int i=0, j=0; FindNext(B); while(i<A) { while(j==-1 || (a[i]==b[j] && i<A && j<B)) i++, j++; if(j==B) { sum++; i+=B-1; } j = Next[j]; } } int main() { while(scanf("%s", a), strcmp(a, "#")) { scanf("%s", b); int A=strlen(a), B=strlen(b); sum = 0; KMP(A, B); printf("%d\n", sum); } return 0; }
标签:
原文地址:http://www.cnblogs.com/YY56/p/4833958.html