BZOJ 3676: [Apio2014]回文串 回文树 回文自动机

时间:2023-04-25 17:45:02

http://www.lydsy.com/JudgeOnline/problem.php?id=3676

另一种更简单更快常数更小的写法,很神奇……背板子。

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
const int maxn=;
char ch[maxn];
int siz;
struct pam{
int sig[];
int f,len,cnt;
}t[maxn];
int tot=,las=;
long long ans=;
void add(int z,int n){
int p=las;
while(ch[n-t[p].len-]!=ch[n])p=t[p].f;
if(!t[p].sig[z]){
int y=++tot;int k=t[p].f;
t[y].len=t[p].len+;
while(ch[n-t[k].len-]!=ch[n])k=t[k].f;
t[y].f=t[k].sig[z];t[p].sig[z]=y;//注意这里的顺序是不能调整的
}
las=t[p].sig[z];
t[las].cnt++;
}
void solve(){
for(int i=tot;i;i--){
t[t[i].f].cnt+=t[i].cnt;
ans=max(ans,(long long )t[i].cnt*t[i].len);
}
}
int main(){
memset(t,,sizeof(t));t[].f=t[].f=;t[].len=-;
scanf("%s",ch+);siz=strlen(ch+);
for(int i=;i<=siz;i++)add(ch[i]-'a',i);
solve();
printf("%lld\n",ans);
return ;
}