//#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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-12-20 17:17:14