#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=0;i<n;i++)
#define rep(i,n) for(i=1;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int f[10005];
char p[10005],t[1000005];
int i;
int m;
void getFail(char *p,int *f)
{
m=strlen(p);
f[0]=0;
f[1]=0;
rep(i,m)
{
int j=f[i];
while(j&&p[i]!=p[j])
{
j=f[j];
}
f[i+1]=p[i]==p[j]?j+1:0;
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,ans;
cin>>n;
while(n--)
{
cin>>p;
cin>>t;
getFail(p,f);
ans=0;
int k=strlen(t);
int j=0;
reP(i,k)
{
while(j&&t[i]!=p[j])j=f[j];
if(p[j]==t[i])j++;
if(j==m){ans++;
j=f[j];
}
}
cout<<ans<<endl;
}
return 0;
}
hiho #1015 : KMP算法
最新推荐文章于 2021-03-05 12:40:00 发布
http://hihocoder.com/problemset/problem/1015
kmp基础题