题面
https://vjudge.net/problem/POJ-3461
题解
#include<cstdio> #include<iostream> #include<cstring> #define ri register int #define N 1000500 using namespace std; int T,next[N],n,m; char s[N],t[N]; void getnext(){ next[0]=next[1]=0; for (ri i=2;i<=n;i++) { int j=next[i-1]; while (j && s[j+1]!=s[i]) j=next[j]; if (s[j+1]==s[i]) next[i]=j+1; else next[i]=0; } } int main(){ scanf("%d",&T); while (T--) { scanf("%s",s+1); n=strlen(s+1); scanf("%s",t+1); m=strlen(t+1); getnext(); int k=0; int sum=0; for (ri i=1;i<=m;i++) { while (k && s[k+1]!=t[i]) k=next[k]; if (s[k+1]==t[i]) { k++; if (k==n) { k=next[k]; sum++; } } } printf("%d ",sum); } return 0; }