回文树的裸题。
#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; }
时间: 2024-10-13 20:50:08