便是还是不能理解KMP的代码的运行机理,先套模板吧>_<
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
#define clr(x,y) memset(x,y,sizeof x)
typedef long long ll;
const int maxn = 1000000 + 2;
const ll Mod = 1000000007;
#define INF 0x3f3f3f3f
#define P pair<int,int>
char p[maxn/ 100],t[maxn];
int next[maxn];
void get_next()
{
int m = strlen(p);
next[0] = next[1] = 0;
for(int i = 1; i < m; i ++)
{
int j = next[i];
while(j && p[i] != p[j])
{
j = next[j];
}
next[i + 1] = p[i] == p[j] ? j + 1 : 0;
}
}
int solve()
{
int ans = 0;
int n = strlen(t),m = strlen(p);
int j = 0;
for(int i = 0; i < n; i ++)
{
while(j && p[j] != t[i])
j = next[j];
if(p[j] == t[i])
j ++;
if(j == m)
ans ++;
}
return ans;
}
int main()
{
int Tcase;
scanf("%d",&Tcase);
while(Tcase --)
{
scanf("%s%s",p,t);
get_next();
printf("%d\n",solve());
}
return 0;
}