蒟蒻开始学回文自动机了
(板子基本靠搬运)
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
struct PAM_Trie {
int ch[26];
int fail, len, num;
};
struct PAM {
PAM_Trie b[N];
int n, last, cnt, s[N];
PAM() {
b[0].len = 0;
b[1].len = -1;
b[0].fail = 1;
b[1].fail = 0;
last = 0;
cnt = 1;
}
int get_fail(int x) {
while(s[n - b[x].len - 1] != s[n]) {
x = b[x].fail;
}
return x;
}
void insert(char ch) {
s[++n]=ch;
int p = get_fail(last);
if(!b[p].ch[s[n]]) {
b[++cnt].len = b[p].len + 2;
int tmp = get_fail(b[p].fail);
b[cnt].fail = b[tmp].ch[s[n]];
b[cnt].num = b[b[cnt].fail].num + 1; //
b[p].ch[s[n]] = cnt;
}
last = b[p].ch[s[n]];
}
} P;
int length;
char c[N];
int main() {
scanf("%s", c + 1);
length = strlen(c + 1);
int k=0;
P.s[0]=26;
for(int i=1;i<=length;i++) {
c[i]=(c[i]-97+k)%26 + 97;
P.insert(c[i]-'a');
printf("%d ",P.b[P.last].num);
k=P.b[P.last].num;
}
}
原文地址:https://www.cnblogs.com/mollnn/p/12252277.html
时间: 2024-11-09 03:47:20