题意:给一个字符串S,求每个i使得前缀字串i等于后缀字串i。
虽然看起来就像是要用KMP的一些思想的,但是看起来还是可以用Hash的。。。
偷懒的我就用Hash写的,RKHash。不过一开始,K取131,P取10000000031时候直接Wa了,之后抱着试一试的心态把K改成31,P改成1000000031(去了一个0),就AC了。 乘爆unsigned long long了?
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef unsigned long long UL;
UL h[400005], mul[400005], K = 31, P = 1000000031;
char s[400005];
int main()
{
mul[0] = 1;
for(int i = 1; i <= 400000; i++) mul[i] = mul[i-1]*K%P;
while(~scanf("%s", s+1))
{
int n = strlen(s+1);
for(int i = 1; i <= n; i++)
{
h[i] = (h[i-1]*K+s[i]-'a'+1)%P;
}
for(int i = 1; i <= n; i++)
{
if(h[i] == ((h[n]-(h[n-i]*mul[i]%P))+P)%P) printf("%d ", i);
}
putchar('\n');
}
return 0;
}