bzoj 3676 [Apio2014]回文串

这道题在洛谷也有(P3649)但数据不好。推荐到bzoj提交

回文自动机的模板题,求出PAM后统计即可。

代码:

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxn = 300000 + 5;

struct PAM
{
	int nxt[maxn][26], link[maxn], len[maxn], size[maxn], sz, last;
	char s[maxn];
	
	void inline extend(int c, int now)
	{
		int p = last;
		while(s[now - len[p] - 1] != s[now]) p = link[p];
		if(!nxt[p][c]) {
			int q = link[p];
			while(s[now - len[q] - 1] != s[now]) q = link[q];
			link[++sz] = nxt[q][c], nxt[p][c] = sz, len[sz] = len[p] + 2;
		}
		last = nxt[p][c]; ++size[last];
	}
	
	void inline build(char *str)
	{
		for(int i = 0; str[i]; ++i) s[i + 1] = str[i];
		link[0] = link[1] = 1; len[0] = 0, len[1] = -1;
		sz = 1;
		for(int i = 0; str[i]; ++i) extend(s[i + 1] - 'a', i + 1);
		for(int i = sz; i > 1; --i) size[link[i]] += size[i];
	}
} pam;

char str[maxn];

void inline Solve()
{
	ll ans = 0;
	pam.build(str);
	for(int i = 2; i <= pam.sz; ++i) ans = max(ans, (ll)pam.size[i] * pam.len[i]);
	printf("%lld\n", ans);
}

int main()
{
	scanf("%s", str);
	Solve();
	return 0;
}
posted @ 2018-12-23 23:32  cjrsacred  阅读(107)  评论(0编辑  收藏  举报