标签:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12000 Accepted Submission(s):
7683
#include<stdio.h> #include<string.h> #include<stdlib.h> const int N = 1010; char Mom[N], Son[N]; int next[N]; int cot; void Get_next(char Son[], int N) { int i = 0, j = -1; next[0] = -1; while(i < N) { if(j == -1 || Son[i] == Son[j]) { i++; j++; next[i] = j; } j = next[j]; } } void KMP(char Mom[], char Son[], int Mn, int Sn) { int i = 0, j = 0; Get_next(Son,Sn); while(i < Mn) { while(j == -1 || (Mom[i] == Son[j] && i < Mn && j < Sn)) { i++; j++; } if(j == Sn) { cot++; j = next[j]; } else j = next[j]; } } int main() { int Mn, Sn; while(scanf("%s", Mom), strcmp(Mom, "#") != 0) { scanf(" %s", Son); Mn = strlen(Mom); Sn = strlen(Son); cot = 0; KMP(Mom, Son, Mn, Sn); printf("%d\n", cot); } return 0; }
标签:
原文地址:http://www.cnblogs.com/Loving140/p/4881099.html