洛谷P3649 [APIO2014]回文串(回文自动机)

传送门

 

话说回文自动机我自己都还没搞懂呢……

等到时候会了再来填坑

 1 //minamoto
 2 #include<cstdio>
 3 #include<cstring>
 4 #define ll long long
 5 template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
 6 const int N=3e5+5;
 7 char s[N];
 8 int n,p,q,fail[N],cnt[N],len[N],tot,last,ch[N][26];
 9 ll ans;
10 inline int newnode(int x){
11     len[++tot]=x;return tot;
12 }
13 inline int getfail(int x,int n){
14     while(s[n-len[x]-1]!=s[n]) x=fail[x];
15     return x;
16 }
17 int main(){
18     scanf("%s",s+1);
19     s[0]=-1,fail[0]=1,last=0;
20     len[0]=0,len[1]=-1,tot=2;
21     for(int i=1;s[i];++i){
22         s[i]-='a';
23         p=getfail(last,i);
24         if(!ch[p][s[i]]){
25             q=newnode(len[p]+2);
26             fail[q]=ch[getfail(fail[p],i)][s[i]];
27             ch[p][s[i]]=q;
28         }
29         ++cnt[last=ch[p][s[i]]];
30     }
31     for(int i=tot;i;--i)
32     cnt[fail[i]]+=cnt[i],cmax(ans,1ll*cnt[i]*len[i]);
33     printf("%lld\n",ans);
34     return 0;
35 }

 

posted @ 2018-09-11 18:43  bztMinamoto  阅读(156)  评论(0编辑  收藏  举报
Live2D