Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

PAM 模板

回文串问题。

查看代码
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
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e6 + 10;
char str[N];
int n, cnt;
struct Node { int p, len, nxt[26]; } tr[N];
int get (int x, int t)
{
while (str[t - 1 - tr[x].len] != str[t]) x = tr[x].p;
return x;
}
void build()
{
scanf("%s", str + 1);
n = strlen(str + 1);
tr[0].p = 1, tr[1].len = -1;
cnt = 2;
for (int i = 1, p = 0; i <= n; ++i)
{
int c = str[i] - 'A';
p = get(p, i);
if (!tr[p].nxt[c])
{
int q = cnt++;
tr[q].len = tr[p].len + 2;
tr[q].p = tr[get(tr[p].p, i)].nxt[c];
tr[p].nxt[c] = q;
}
p = tr[p].nxt[c];
}
}