Seek the Name, Seek the Fame
【题目描述】
给定若干字符串(这些字符串总长 ≤4×105 ),在每个字符串中求出所有既是前缀又是后缀的子串长度。
例如:ababcababababcabab,既是前缀又是后缀的:ab,abab,ababcabab,ababcababababcabab。
【输入】 输入若干行,每行一个字符串。
【输出】 对于每个字符串,输出一行,包含若干个递增的整数,表示所有既是前缀又是后缀的子串长度。
【输入样例】
ababcababababcabab
aaaaa
【输出样例】
2 4 9 18
1 2 3 4 5
#include<bits/stdc++.h>
#define N 400001
#define mod 1000000007
using namespace std;
const int b=233;
typedef unsigned long long ull;
ull power[N],sum[N];
char s[N];
ull _(ull x,ull y)
{
return sum[y]-sum[x-1]*power[y-x+1];
}
int main()
{
power[0]=1;
for(int i=1;i<=40000;++i) power[i]=power[i-1]*b;
while(~scanf("%s",s+1))
{
int len=strlen(s+1);
for(int i=1;i<=len;++i) sum[i]=sum[i-1]*b+s[i];
for(int i=1;i<=len;++i)
if(_(1,i)==_(len-i+1,len)) cout<<i<<' ';
cout<<'\n';
}
return 0;
}
60pts求调
6,8,9,10WA