滑动窗口来比较模式串与子串的哈希值
#include <iostream>
using namespace std;
typedef unsigned long long ULL;
const int N =1000010,P = 131;
ULL h1[N],h2[N],p[N];
int n,m;
char a[N],b[N];
ULL get(int l,int r)
{
return h2[r] - h2[l-1] * p[r - l + 1];
}
int main()
{
scanf("%d%s%d%s",&n,a+1,&m,b+1);
p[0] = 1;
for(int i = 1;i<=n;i++)
h1[i] = h1[i-1]*P + a[i];
for(int i = 1;i<=m;i++)
{
h2[i] = h2[i-1] * P + b[i];
p[i] = p[i-1] * P;
}
for(int l = 1,r = n;r<=m;r++,l++)
{
if(get(l,r) == h1[n]) cout<<l-1<<" ";
}
return 0;
}