Description
给定两个字符串s和t,现有一个扫描器,从s的最左边开始向右扫描,每次扫描到一个t就把这一段删除,输出能发现t的个数。
Input
第一行包含一个整数T(T<=50),表示数据组数。
每组数据第一行包含一个字符串s,第二行一个字符串t,字符串长度不超过1000000。
Output
对于每组数据,输出答案。
Sample Input
2
ababab
ab
ababab
ba
Sample Output
3
2
/*
题意:求模式串在字符串中出现的次数(不可重叠)
类型:KMP
分析:稍微修改一下KMP模版,对于匹配成功时答案数+1,并且使得模式串从头开始
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include <iostream>
#include <string>
using namespace std;
const int maxn = 1e6+5;
int Next[maxn];
char s[maxn],p[maxn];
int n,m;
void getNext(int m){
int i=0,j=-1;
Next[0]=-1;
while(i < m){
if(j == -1 || p[i] == p[j])
Next[++i] = ++j;
else
j=Next[j];
}
}
int Kmp(int n,int m){
getNext(m);
int i = 0,j = 0;
int ans = 0;
while(i < n && j < m){
if(j == -1 || s[i] == p[j]){
i++;
j++;
}
else j=Next[j];
if(j>=m)ans++,j=0;
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%s%s",s,p);
n=strlen(s);m=strlen(p);
int ans=Kmp(n,m);
printf("%d\n",ans);
}
return 0;
}