给两个字符串,问第一个在第二个出现了多少次
kmp
#include <iostream>
#include <map>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#define rep(i, j, k) for(ll i = j; i <= k; i++)
#define ll long long
#define maxn 1000009
#define inf 0x7fffffff
using namespace std;
int n, m;
char a[maxn], b[maxn];
int Next[maxn];
void pre ()
{
Next[0] = Next[1] = 0;
rep (i, 1, m - 1)
{
int j = Next[i];
while (j && b[i] != b[j])
j = Next[j];
Next[i + 1] = b[i] == b[j] ? j + 1 : 0;
}
}
int work ()
{
int j = 0, ret = 0;
rep (i, 0, n - 1)
{
while (j && b[j] != a[i])
j = Next[j];
if (b[j] == a[i])
j++;
if (j == m)
ret++;
}
return ret;
}
int main ()
{
int ti;
cin >> ti;
while (ti--)
{
scanf ("%s", b);
scanf ("%s", a);
m = strlen (b);
n = strlen (a);
pre ();
cout << work () << endl;
}
return 0;
}