求字符串首位一样的所有长度
Solution
对字符串建立hash, 枚举长度get_hash比较即可
Code
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<climits>
#define LL long long
#define REP(i, x, y) for(LL i = (x);i <= (y);i++)
using namespace std;
LL RD(){
LL out = 0,flag = 1;char c = getchar();
while(c < '0' || c >'9'){if(c == '-')flag = -1;c = getchar();}
while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
return flag * out;
}
const LL maxn = 400010, mod = 1e9 + 9, base = 233;
char s[maxn];
LL p[maxn], hash[maxn], len;
void init(){
p[0] = 1;
REP(i, 1, maxn - 5)p[i] = p[i - 1] * base % mod;
}
LL get_hash(LL l, LL r){
return ((hash[r] - hash[l - 1] * p[r - l + 1] % mod) % mod + mod) % mod;
}
void work(){
len = strlen(s + 1);
hash[0] = 0;
REP(i, 1, len)hash[i] = (hash[i - 1] * base % mod + s[i] - 'a') % mod;
bool first = 1;
REP(i, 1, len){
if(get_hash(1, i) == get_hash(len - i + 1, len)){
if(first)printf("%d", i), first = 0;
else printf(" %d", i);
}
}
puts("");
}
int main(){
init();
while(scanf("%s", s + 1) != EOF){
work();
}
return 0;
}