回文串【manacher算法和回文自动机、回文树】 Summary
知识点总结
Problems
Hdu5785 Interesting(Manacher)
Hdu5157 Harry and magic string(回文自动机)
Hdu5157 Harry and magic string(回文自动机)
Hdu5421 Victor and String(回文自动机)
Hdu5421 Victor and String(回文自动机)
Hdu3068 最长回文
Manacher模板题
Code
1 |
|
[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
63
64
65
66
67
68
69
70
71
72#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int Maxn = 300000 + 100;
char S[Maxn];
int N, cnt;
struct tree
{
int cnt, ch[30], len, fail;
};
namespace PAM
{
tree Tree[Maxn];
int root0, root1, now;
inline void init ()
{
cnt = -1;
root0 = ++cnt, root1 = ++cnt;
Tree[root0].fail = root1;
Tree[root0].len = 0;
Tree[root1].len = -1;
}
inline int get_fail (int now, int i)
{
while (S[i - Tree[now].len - 1] != S[i]) now = Tree[now].fail;
return now;
}
inline void add (int c, int i)
{
now = get_fail(now, i);
if (!Tree[now].ch[c])
{
int x = ++cnt;
Tree[x].len = Tree[now].len + 2;
Tree[x].fail = Tree[get_fail(Tree[now].fail, i)].ch[c];
Tree[now].ch[c] = x;
}
now = Tree[now].ch[c];
Tree[now].cnt ++;
}
}
int main()
{
#ifdef hk_cnyali
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#endif
scanf("%s", S);
PAM :: init();
int N = strlen(S);
for (int i = 0; i < N; ++i)
PAM :: add(S[i] - 'a', i);
LL Ans = 0;
for (int i = cnt; i > 1; --i)
{
PAM :: Tree[PAM :: Tree[i].fail].cnt += PAM :: Tree[i].cnt;
Ans = max(Ans, (LL)((LL)PAM :: Tree[i].cnt * PAM :: Tree[i].len));
}
cout<<Ans<<endl;
return 0;
}