bzoj3676: [Apio2014]回文串 回文树
回文树的裸题。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; typedef long long ll; const int maxn=500100; const int INF=1e9+10; struct PalinTree { int ch[maxn][26],f[maxn]; int cnt[maxn],num[maxn],len[maxn]; int s[maxn]; int last,n,tot; int newnode(int l) { MS0(ch[tot]); cnt[tot]=0; num[tot]=0; len[tot]=l; return tot++; } void init() { tot=0; newnode(0); newnode(-1); last=0;n=0; s[n]=-1;f[0]=1; } int get_fail(int x) { while(s[n-len[x]-1]!=s[n]) x=f[x]; return x; } void add(int c) { c-='a'; s[++n]=c; last=get_fail(last); if(!ch[last][c]){ int cur=newnode(len[last]+2); f[cur]=ch[get_fail(f[last])][c]; ch[last][c]=cur; num[cur]=num[f[cur]]+1; } last=ch[last][c]; cnt[last]++; } void count() { for(int i=tot-1;i>=0;i--) cnt[f[i]]+=cnt[i]; } ll work() { ll ans=0; REP(i,0,tot-1) ans=max(ans,1LL*len[i]*cnt[i]); return ans; } };PalinTree pt; char s[maxn]; int main() { freopen("in.txt","r",stdin); while(~scanf("%s",s)){ int len=strlen(s); pt.init(); REP(i,0,len-1) pt.add(s[i]); pt.count(); printf("%lld\n",pt.work()); } return 0; }
没有AC不了的题,只有不努力的ACMER!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步