/*POJ 3461*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char str[1000010];
char pat[10010];
int next[10010];
int kmp(int, int, int &);
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int i, j, slen, plen,count;
scanf("%s",pat);
scanf("%s",str);
slen = strlen(str);
plen = strlen(pat);
i = 0;
j = -1;
next[i] = -1;
for( ;i < plen ;)
if(j == -1 || pat[i] == pat[j])
{
++i;
++j;
if(pat[i] != pat[j])
next[i] = j;
else
next[i] =next[j];
}
else
j = next[j];
count = 0;
kmp(slen, plen, count);
printf("%d/n",count);
}
//system("pause");
return 0;
}
int kmp(int slen, int plen, int &count )
{
int i,j;
j = 0;
i = 0;
while(i < slen && j < plen)
{
if(j == -1 || str[i] == pat[j])
{
++i;
++j;
}
else
j = next[j];
if(j == plen)
{
++count;
j = next[j];
}
}
return count;
}
POJ 3461
最新推荐文章于 2023-06-25 08:42:03 发布