题目:
题解:
KMP板子
用失配函数-----不相等的话向前缀跳
文本字符串中单词出现的所有位置
最短循环节
#include <cstdio>
#include <cstring>
using namespace std;
int t[10005],l1,l2;
char s1[10005],s2[1000005];
void sp()
{
int j=0,i;
t[0]=-1;
for (i=0;i<l1;i++)
{
j=t[i];
while (j!=-1 && s1[i]!=s1[j]) j=t[j];
t[i+1]=++j;
}
}
int main()
{
int T,i;
scanf("%d\n",&T);
while (T--)
{
gets(s1);
gets(s2);
l1=strlen(s1);
l2=strlen(s2);
sp();
int j=0,ans=0;
for (i=0;i<l2;i++)
{
while (j!=-1 && s2[i]!=s1[j]) j=t[j];
j++;
if (j==l1)
{
ans++; j=t[j];
}
}
printf("%d\n",ans);
}
}