【BZOJ3676】[Apio2014]回文串 【回文自动机】

【题目链接】

学习一发回文自动机!

照着hzwer的模板打了一发

/* Pigonometry */
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

const int maxn = 300005;

char str[maxn];

struct _pam {
	int son[maxn][26], fail[maxn], len[maxn], size[maxn], cnt, last;

	void init() {
		memset(son, 0, sizeof(son));
		for(int i = 0; i < maxn; i++) fail[i] = len[i] = size[i] = 0;
		fail[0] = fail[1] = 1;
		len[0] = 0; len[1] = -1;
		cnt = 1; last = 0;
	}

	void insert(int c, int n) {
		int now = last;
		for(; str[n - 1 - len[now]] != str[n]; now = fail[now]);
		if(!son[now][c]) {
			int pos = ++cnt;
			len[pos] = len[now] + 2;
			int k = fail[now];
			for(; str[n - 1 - len[k]] != str[n]; k = fail[k]);
			fail[pos] = son[k][c];
			son[now][c] = pos;
		}
		last = son[now][c];
		size[last]++;
	}

	LL solve() {
		LL res = 0;
		for(int i = cnt; i; i--) {
			size[fail[i]] += size[i];
			res = max(res, (LL)size[i] * len[i]);
		}
		return res;
	}
} pam;

int main() {
	scanf("%s", str + 1); int n = strlen(str + 1);
	pam.init();
	for(int i = 1; i <= n; i++) pam.insert(str[i] - 'a', i);
	printf("%lld\n", pam.solve());
	return 0;
}


你可能感兴趣的:(【BZOJ3676】[Apio2014]回文串 【回文自动机】)