/* I will wait for you */
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<string>
#define make make_pair
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int maxn=500010;
const int maxm=1010;
const int maxs=26;
const int INF=1<<29;
const int P=1000000007;
const double error=1e-9;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') f=(ch=='-'?-1:1),ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
struct pam
{
pam *fa,*next[maxs];int val,len;
}su[maxn],*headf,*heads,*last;
char s[maxn];int n,pos,cnt;LL ans;
void init()
{
headf=&su[++cnt],last=heads=&su[++cnt];
heads->len=-1,headf->fa=heads;
}
void add(int x)
{
pam *per=last;
for(pos++;s[pos-per->len-1]!=s[pos];per=per->fa);
if(!per->next[x])
{
pam *now=&su[++cnt];
now->val=1,now->len=per->len+2;
last=per->next[x]=now;
if(per==heads) now->fa=headf;
else
{
for(per=per->fa;s[pos-per->len-1]!=s[pos];per=per->fa);
now->fa=per->next[x];
}
}
else (last=per->next[x])->val++;
}
int main()
{
scanf("%s",s+1),n=strlen(s+1),init();
for(int i=1;i<=n;i++) add(s[i]-'a');
for(pam* i=&su[cnt];i!=heads;i--)
i->fa->val+=i->val,ans=max(ans,(LL)i->len*i->val);
printf("%lld\n",ans);
return 0;
}
BZOJ3676【回文自动机】
最新推荐文章于 2019-11-23 21:24:51 发布
这是一个关于如何使用回文自动机(PAM)解决字符串处理问题的代码实现。程序首先初始化回文自动机,然后读取输入字符串,逐字符添加到自动机中,并更新每个节点的值和长度。最后,计算并输出回文子串的最大价值。
摘要由CSDN通过智能技术生成