标签:
#include<stdio.h>
#include<string.h>
#define N 10010
char str1[N],str2[N*100];
int cent,i,j;
int len1,len2;
int p[N];
void getp()
{
i=0,j=-1;
p[i]=j;
while(i<len1)
{
if(j==-1||str1[i]==str1[j])
{
i++,j++;
p[i]=j;
}
else
j=p[j];
}
}
void kmp()
{
getp();
i=0,j=0;
while(i<len2)
{
if(j==-1||str2[i]==str1[j])
{
i++,j++;
if(j==len1)
cent++;
}
else
j=p[j];
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
cent=0;
scanf("%s%s",str1,str2);
len1=strlen(str1);
len2=strlen(str2);
kmp();
printf("%d\n",cent);
}
return 0;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/zm_11/article/details/47337247