#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 9;
struct PAM {
struct Node {
int tr[26], len, fail, cnt, sta, sum;
} pa[N];
int last, tot;
void init( ) {
last = 0, tot = 1;
pa[1].len = -1, pa[0].len = 0;
pa[0].fail = 1, pa[1].fail = 0;
}
int getfail(int x, int i, string &s) {
while(i - pa[x].len - 1 < 0 || s[i - pa[x].len - 1] != s[i]) x = pa[x].fail;
return x;
}
int insert(string& s, int r) {
int c = s[r] - 'a';
int p = getfail(last, r, s);
int np = pa[p].tr[c];
if (!np) {
np = ++tot;
pa[np].len = pa[p].len + 2;
int nq = pa[p].fail;
nq = getfail(nq, r, s);
pa[np].fail = pa[nq].tr[c];
pa[p].tr[c] = np;
pa[tot].sum = pa[pa[tot].fail].sum + 1;
}
pa[np].cnt++;
last = np;
return pa[np].sum;
}
int las;
void build(string& s) {
for (int i = 0; i < s.size(); i++) {
s[i] = (s[i] - 97 + las) % 26 + 97;
las = insert(s, i);
cout << las << " ";
}
}
} pam;
int main() {
string s;
cin >> s;
pam.init( );
pam.build(s);
return 0;
}
P5496 【模板】回文自动机(PAM)
最新推荐文章于 2024-08-21 18:16:36 发布