题目大意:题目叙述很多,其实只看输入输出也能明白什么意思,给两个串W,T, 判断T串中包含几个串W。
分析:还是基础的KMP应用.......................
直接上代码。
==================================================================================================================
#include<stdio.h> #include<string.h> const int MAXM = 1e4+7; const int MAXN = 1e6+7; void GetNext(char s[], int next[], int N) { int i=0, j=-1; next[0] = -1; while(i < N) { if(j==-1 || s[i]==s[j]) next[++i] = ++j; else j = next[j]; } } int KMP(char W[], char T[], int Nw, int Nt) { static int next_w[MAXM]; int i=0, j=0, ans=0; GetNext(W, next_w, Nw); while(i < Nt) { while(j==-1 || (T[i] == W[j] && i<Nt) ) i++, j++; if(j == Nw) ans++; j = next_w[j]; } return ans; } int main() { int ncase; scanf("%d", &ncase); while(ncase--) { static char W[MAXM], T[MAXN]; int Nw, Nt; scanf("%s%s", W, T); Nw = strlen(W); Nt = strlen(T); int ans = KMP(W, T, Nw, Nt); printf("%d ", ans); } return 0; }