没看见有人用哈希做的,发一下哈希代码,思路就是算出每一段哈希值,进行比较;(太弱不会KMP)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char s[500009];
unsigned long long int power[500009];
unsigned long long int sum[500009];
int main()
{
int b=233;
power[0]=1;
for(int i=1;i<=500009;i++)
power[i]=power[i-1]*b;
while(scanf("%s",s+1)!=EOF)
{
int m=strlen(s+1);
for(int i=1;i<=m;i++)
sum[i]=sum[i-1]*b+(unsigned long long int)(s[i]-'A'+1);
for(int i=m;i>=1;i--)
{
unsigned long long int k=sum[m]-sum[i-1]*power[m-i+1];
if(k==sum[m-i+1])
{
cout<<m-i+1<<" ";
}
}
printf("\n");
}
return 0;
}