#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
char str[maxn],mo[maxn];
int Next[maxn];
void getnext()
{
int i=0,j=-1,len=strlen(mo);
while(i<len)
{
if(j==-1||mo[i]==mo[j]){Next[++i]=++j;}
else j = Next[j];
}
}
int KMP()
{
int i=0,j=0,l1=strlen(str),l2=strlen(mo);
int ans=0;
while(i<l1)
{
if(j==-1||mo[j]==str[i])i++,j++;
else j=Next[j];
if(j==l2)ans++;
}
return ans;
}
int main()
{
int t;
scanf("%d,",&t);
while(t--)
{
scanf("%s %s",mo,str);
Next[0]=-1;
getnext();
printf("%d\n",KMP());
}
return 0;
}
hdu1686 KMP算法
最新推荐文章于 2023-03-12 17:47:09 发布