#include <iostream>
#include <cstdio>
#include <string.h>
#define MAX 10010
#define MAXM 1000010
using namespace std;
int next[MAX];
void getNext(char w[])
{
next[0] = -1;
next[1] = 0;
int i = 0, j = -1;
while (i < strlen(w))
{
if (j == -1 || w[j] == w[i])
next[++i] = ++j;
else
j = next[j];
}
}
int main()
{
//freopen("aa.txt", "r", stdin);
char w[MAX], t[MAXM];
int n;
scanf("%d", &n);
while (n--)
{
scanf("%s", w);
scanf("%s", t);
getNext(w);
int len1 = strlen(t), len2 = strlen(w);
int count = 0, j = 0, i = 0;
while (i < len1)
{
if (j == -1 || w[j] == t[i])
{
i++;
j++;
}
else
j = next[j];
if (j == len2)
{
count++;
j = next[j];
}
}
printf("%d\n", count);
}
}
POJ_3460_Oulipo_KMP
最新推荐文章于 2020-11-13 10:50:41 发布