//#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
//string a,b;
char a[10000],b[1000000];
int asize,bsize;
int kmp(){
int *pi = new int [asize];
pi[0] = -1;
for(int i = 1,k = -1;i<asize;i++){
while(k>-1&&a[k+1] != a[i])
k = pi[k];
if(a[k+1] == a[i])
k++;
pi[i] = k;
}
int result = 0;
for(int i = 0,k = -1;i<bsize;i++){
while(k>-1&&a[k+1] != b[i])
k = pi[k];
if(a[k+1] == b[i])
k++;
if(k == asize - 1){
result++;
k = pi[k];
}
}
return result;
}
int main(){
int times;
//cin >> times;
scanf("%d",×);
while(times--){
scanf("%s%s",a,b);
asize = strlen(a);
bsize = strlen(b);
printf("%d\n",kmp());
//a.clear();
//b.clear();
//cin >> a >> b;
//cout << kmp() << endl;
}
return 0;
}
poj 3461 Oulipo kmp字符串匹配
最新推荐文章于 2020-04-08 23:11:59 发布