1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| namespace pam { int sz, last1, last2, l, r; int ch[maxn][26], len[maxn], fail[maxn]; int cnt[maxn], dep[maxn]; char s[maxn]; int node(int l) { sz++; ms(ch[sz], 0); len[sz] = l; fail[sz] = cnt[sz] = dep[sz] = 0; return sz; } void clear() { sz = -1; last1 = last2 = 0; l = 100002; r = l - 1; node(0); node(-1); fail[0] = 1; ms(s, -1); } int getfail1(int x) { while (s[r - len[x] - 1] != s[r]) x = fail[x]; return x; } int getfail2(int x) { while (s[l + len[x] + 1] != s[l]) x = fail[x]; return x; } int insertR(char c) { s[++r] = c; int now = getfail1(last1); if (!ch[now][c - 'a']) { int x = node(len[now] + 2); fail[x] = ch[getfail1(fail[now])][c - 'a']; dep[x] = dep[fail[x]] + 1; ch[now][c - 'a'] = x; } last1 = ch[now][c - 'a']; cnt[last1]++; if (len[last1] == r - l + 1) last2 = last1; return dep[last1]; } int insertL(char c) { s[--l] = c; int now = getfail2(last2); if (!ch[now][c - 'a']) { int x = node(len[now] + 2); fail[x] = ch[getfail2(fail[now])][c - 'a']; dep[x] = dep[fail[x]] + 1; ch[now][c - 'a'] = x; } last2 = ch[now][c - 'a']; cnt[last2]++; if (len[last2] == r - l + 1) last1 = last2; return dep[last2]; } }
|