#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
const int N =1e6+100;
char s[N], p[N];
int m, n,ne[N];
int main()
{
cin >> n >> p + 1 >> m >> s + 1;
for (int i = 2,j=0; i <= n; i++)
{
while (j && p[i] != p[j + 1]) j = ne[j];
if (p[i] == p[j + 1])
j++;
ne[i] = j;
}
for (int i = 1, j = 0; i <= m; i++)
{
while (j && s[i] != p[j + 1]) j = ne[j];
if (s[i] == p[j + 1])
j++;
if (j == n)
{
printf("%d ", i - n);
j = ne[j];
}
}
return 0;
}
kmp模板
最新推荐文章于 2024-09-10 15:28:21 发布