Blog of RuSun

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

P3649 [APIO2014] 回文串

P3649 [APIO2014] 回文串

回文树板子题。

查看代码
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
57
58
59
60
61
62
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0';
flag && (x = ~x + 1);
}
template <class Type, class ...rest>
void read (Type &x, rest &...y) { read(x), read(y...); }
template <class Type>
void write (Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar('0' + x % 10);
}
typedef long long LL;
const int N = 3e5 + 10;
char str[N];
struct Node { int p, len, s, nxt[26]; } tr[N];
int n, cnt, last;
int get (int x, int t)
{
while (str[t - tr[x].len - 1] != str[t]) x = tr[x].p;
return x;
}
int main ()
{
scanf("%s", str + 1); str[0] = '$';
n = strlen(str + 1);
tr[0].p = 1, tr[1].len = -1;
last = 0, cnt = 2;
for (int i = 1; i <= n; ++i)
{
int c = str[i] - 'a', p = get(last, 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;
}
++tr[last = tr[p].nxt[c]].s;
}
LL res = 0;
for (int i = cnt - 1; i; --i)
{
tr[tr[i].p].s += tr[i].s;
res = max(res, (LL)tr[i].len * tr[i].s);
}
write(res);
return 0;
}