您的位置:首页 > 其它

[BZOJ3676][Apio2014]回文串(回文自动机)

2016-12-19 23:39 441 查看

题目描述

传送门

题解

回文自动机裸题。

不知道什么是回文自动机的安利一下

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define N 300010
#define LL long long
char s
;
int n,now,cur,fail[N],cnt[N],len[N],tot,last,ch[N][26];
LL ans;

int newnode(int x)
{
len[tot]=x;
return tot++;
}
int get_fail(int x,int n)
{
while(s[n-len[x]-1]!=s
) x=fail[x];
return x;
}
LL Max(LL a,LL b)
{
return (a>b)?a:b;
}
int main(){
gets(s+1);
s[0]=-1;newnode(0);newnode(-1);fail[0]=1;
for(n=1;s
;++n)
{
s
-='a';
cur=get_fail(last,n);
if (!ch[cur][s
])
{
now=newnode(len[cur]+2);
fail[now]=ch[get_fail(fail[cur],n)][s
];
ch[cur][s
]=now;
}
cnt[last=ch[cur][s
]]++;
}
for(n=--tot;n>1;--n)
cnt[fail
]+=cnt
,ans=Max(ans,(LL)cnt
*(LL)len
);
printf("%lld\n",ans);
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: