题目大意
求在一个字符串内,一个回文子串的长度*出现次数的最大值
回文自动机PAM的模板题
建树的时候统计一下当前节点表示的回文串出现的次数
最后扫一遍更新答案,记得$for$时要把值上传
My complete code:
#include<cstdio> #include<string> #include<cstring> using namespace std; typedef long long LL; const LL maxn=300100; struct node{ LL son[26],fail,len,val; }tree[maxn]; LL len,ans,last,nod; char s[maxn]; int main(){ scanf(" %s",s+1); len=strlen(s+1); for(LL i=1;i<=len;++i) s[i]-='a'; s[0]='#'; tree[0].fail=1; tree[0].len=0; tree[1].fail=0; tree[1].len=-1; last=0; nod=1; for(LL i=1;i<=len;++i){ while(s[i-tree[last].len-1]!=s[i]) last=tree[last].fail; if(!tree[last].son[s[i]]){ tree[++nod].len=tree[last].len+2; LL j=tree[last].fail; while(s[i-tree[j].len-1]!=s[i]) j=tree[j].fail; tree[nod].fail=tree[j].son[s[i]]; tree[last].son[s[i]]=nod; } last=tree[last].son[s[i]]; ++tree[last].val; } for(LL i=nod;i>=2;--i){ tree[tree[i].fail].val+=tree[i].val; if(tree[i].val*tree[i].len>ans) ans=tree[i].val*tree[i].len; } printf("%lld",ans); return 0; }