题解:
简单kmp
然而strlen时间号费啊
代码:
#include<cstdio> #include<cstring> using namespace std; const int N=1000005; #define next ____next int next[N],ans,len,lens,lenp,T; char s1[N],s2[N]; void get_next(char a[]) { int i=-1,j=0; next[0]=-1; while (j<lenp) { if (i==-1||a[i]==a[j])next[++j]=++i; else i=next[i]; } } void KMP(char a[],char b[]) { int i=0,j=0; while (i!=lens&&j!=lenp) { if (j==-1||a[j]==b[i])i++,j++; else j=next[j]; if (j==lenp) { ans++; j=next[j]; } } } int main() { scanf("%d",&T); while (T--) { scanf("%s%s",&s1,&s2); lens=strlen(s2);lenp=strlen(s1); get_next(s2); ans=0; KMP(s1,s2); printf("%d ",ans); } }