回文自动机 PAM

int n; char s[N];
struct PAM {
	int son[26], len, fail;
} p[N];
int tot = 1, id[N];
il int getf(int x, int i) {
	while (i - p[x].len - 1 < 0 || s[i - p[x].len - 1] != s[i]) 
		x = p[x].fail;
	return x;
}
il void ist(int i) {
	int pos = getf(id[i - 1], i), ch = s[i] - 'a';
	if (!p[pos].son[ch]) {
		p[++tot].fail = p[getf(p[pos].fail, i)].son[ch];
		p[pos].son[ch] = tot;
		p[tot].len = p[pos].len + 2;
	}
	id[i] = p[pos].son[ch];
}
signed main() {
	ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
	cin >> (s + 1);
	n = strlen(s + 1);
	p[0].fail = 1, p[1].len = -1;
	for (int i = 1; i <= n; i++) 
		ist(i);
	
	return 0;
}
posted @ 2026-10-12 15:38  Zctf1088  阅读(11)  评论(0)    收藏  举报