BZOJ 3676: [Apio2014]回文串 回文自动机
作者:互联网
Code:
#include <cstdio> #include <cstring> #include <algorithm> #define ll long long #define N 300005 #define maxn 300005 #define setIO(s) freopen(s".in","r",stdin) using namespace std; int str[maxn],n; char s[maxn]; ll ans; struct PAM{ int fail[N],cnt[N],len[N],ch[N][30],last,tot; void init(){ str[0] = -1,fail[0] = 1,last = 0; len[0] = 0,len[1] = -1, tot = 1; } int newnode(int x) { len[++tot] = x; return tot; } int getfail(int x,int nn){ while(str[nn - len[x] - 1] != str[nn]) x = fail[x]; return x; } void ins(int c,int i) { int p = getfail(last,i); if(!ch[p][c]) { int q = newnode(len[p] + 2); fail[q] = ch[getfail(fail[p],i)][c]; ch[p][c] = q; } ++cnt[last = ch[p][c]]; } void solve(){ for(int i = tot;i >= 1; --i) { ans = max(ans,(ll)cnt[i]*len[i]); cnt[fail[i]] += cnt[i]; } } }T; int main(){ //setIO("input"); scanf("%s",s + 1),n = strlen(s + 1),T.init(); for(int i = 1;i <= n; ++i) str[i] = s[i] - 'a'; for(int i = 1;i <= n; ++i) T.ins(str[i],i); T.solve(); printf("%lld\n",ans); return 0; }
标签:cnt,ch,tot,int,len,Apio2014,fail,3676,回文 来源: https://www.cnblogs.com/guangheli/p/10361322.html