BZOJ3676 [Apio2014]回文串

传送门

回文自动机板子题~

回文自动机和ACA以及SAM都是很类似的[毕竟都是自动机吗233]

回文自动机的树形结构是 fail指针构成的

用增量法 构造即可

(其实我也没完全学懂呢T^T)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define inf 20021225
#define ll long long
#define mxn 310000
using namespace std;

struct node{int ch[26],len,fa,s;}t[mxn];
// ch add character fa fail s times
char ch[mxn]; int lt,poi,s[mxn],n; ll ans;
void init()
{
	s[0]=-1;
	t[0].len=0; t[1].len=-1;
	t[0].fa=1; t[1].fa=1;
	poi=lt=1;
}
int id(char c){return c-'a';}
void extend(int c)
{
	int p=lt; s[++n]=c;
	while(s[n-1-t[p].len] != s[n]) p=t[p].fa;
	if(!t[p].ch[c])
	{
		int q=++poi; t[q].len=t[p].len+2;int np = t[p].fa;
		while(s[n-1-t[np].len] != s[n]) np=t[np].fa;
		t[q].fa=t[np].ch[c]; t[p].ch[c]=q; 
	}
	lt=t[p].ch[c]; t[lt].s++;
}
void calc()
{
	for(int i=poi;~i;i--)	t[t[i].fa].s += t[i].s;
	for(int i=0;i<=poi;i++)	ans=max(ans,1ll*t[i].len*t[i].s);
}
int main()
{
	scanf("%s",ch); init(); int n = strlen(ch);
	for(int i=0;i<n;i++)	extend(id(ch[i]));
	calc(); printf("%lld\n",ans);
	return 0;
}

转载于:https://www.cnblogs.com/hanyuweining/p/10321912.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值