http://www.lydsy.com:808/JudgeOnline/problem.php?id=3676
这是一道回文树裸题,具体如何建图见http://blog.csdn.net/u013368721/article/details/42100363
code:
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #define maxn 300005 6 using namespace std; 7 typedef long long int64; 8 char s[maxn]; 9 struct ptree{ 10 int n,last,idx,s[maxn],son[maxn][26],fai[maxn],len[maxn],cnt[maxn]; 11 void init(){n=0,s[0]=-1,idx=1,last=0,fai[0]=1,len[1]=-1;} 12 int get_fai(int x){ 13 while (s[n-len[x]-1]!=s[n]) x=fai[x]; 14 return x; 15 } 16 void insert(int ch){ 17 s[++n]=ch; 18 int p=get_fai(last); 19 if (!son[p][ch]){ 20 len[++idx]=len[p]+2; 21 int q=idx; 22 fai[q]=son[get_fai(fai[p])][ch]; 23 son[p][ch]=q; 24 } 25 cnt[last=son[p][ch]]++; 26 } 27 void count(){for (int i=idx;i>=0;i--) cnt[fai[i]]+=cnt[i];} 28 void query(){ 29 int64 ans=0; 30 count(); 31 for (int i=0;i<=idx;i++) ans=max(ans,1LL*cnt[i]*len[i]); 32 printf("%lld ",ans); 33 } 34 }T; 35 int main(){ 36 scanf("%s",s+1); T.init(); 37 for (int i=1;s[i];i++) T.insert(s[i]-'a'); 38 T.query(); 39 return 0; 40 }