#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+10,M = 1e6+10;
int n, m;
int ne[N];
char s[M], p[N];
int main()
{
ios::sync_with_stdio(false);
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(s[i] != p[j+1] && j)j = ne[j];
if(s[i] == p[j+1])j++;
if(j == n){
cout << i - n << " ";
j = ne[j];
}
}
return 0;
}
kmp算法
于 2021-08-24 09:21:19 首次发布