题目链接:http://poj.org/problem?id=3461
解题思路:
裸KMP。
可以参考 ---> https://blog.csdn.net/weixin_43768644/article/details/90034165
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1e6+5;
char s[N],ss[10000+5];
int fail[N];
int kmp(int len,int len2)
{
int cnt = 0;
fail[0] = -1;
for (int i=0,j=-1;i<len2;){
if (j==-1||ss[i]==ss[j]){
i++;
j++;
fail[i] = j;
}
else j = fail[j];
}
for (int i=0,j=0;i<len;){
if (j==-1||s[i]==ss[j]){
i++;
j++;
}
else j = fail[j];
if (j==len2){
cnt++;
}
}
return cnt;
}
int main()
{
int T;
scanf("%d",&T);
while (T--){
scanf("%s",ss);
scanf("%s",s);
printf("%d\n",kmp(strlen(s),strlen(ss)));
}
return 0;
}