- 题目描述
给定字符串 、 ,求 在 中的出现次数。 和 中的字符均为英文大写字母或小写字母。 - 中不同位置出现的 可重叠。
- 输入格式
输入共两行,分别是字符串 和字符串 。 - 输出格式
输出一个整数,表示 在 中的出现次数。 - 样例输入
zyzyzyz
zyz - 样例输出
3
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 100010, M = 1000010;
int lc, ls, ans;
int ne[N];
char s[M], c[N];
int main()
{
scanf("%s %s", s + 1, c + 1);
s[0] = '0', c[0] = '0';
lc = strlen(c) - 1, ls = strlen(s) - 1;
for (int i = 2, j = 0; i <= lc; i ++ ){
while (j && c[i] != c[j + 1]) j = ne[j];
if (c[i] == c[j + 1]) j ++ ;
ne[i] = j;
}
for (int i = 1, j = 0; i <= ls; i ++ ){
while (j && s[i] != c[j + 1]) j = ne[j];
if (s[i] == c[j + 1]) j ++ ;
if (j == lc){
ans ++;
j = ne[j];
}
}
cout << ans;
return 0;
}