22222
作者:
Hasity
,
2020-12-15 18:09:46
,
所有人可见
,
阅读 528
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1000010;
int n, m;
string p, s;
int ne[N];
int main()
{
cin >> m >> p >> n >> s;
ne[0] = 0;
ne[1] = 0;
int i = 2, j = 0;
for (i = 2; i < p.size() + 1; i++)
{
while (j && p[i - 1] != p[j]) j = ne[j];
if (j != 0)
{
ne[i] = j + 1;
j++;
}
else
{
if (p[i - 1] == p[j])
{
ne[i] = 1;
j++;
}
else
{
ne[i] = 0;
}
}
}
for (i = 0, j = 0; i < s.size(); i++)
{
while (j && s[i] != p[j])
j = ne[j];
if (j != 0)
{
j++;
}
else
{
if (s[i] == p[j])
j++;
}
if (j == p.size())
{
cout << i - j + 1 << " ";
}
}
}