-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManacher.cpp
More file actions
59 lines (59 loc) · 941 Bytes
/
Manacher.cpp
File metadata and controls
59 lines (59 loc) · 941 Bytes
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
class Manacher //https://github.com/seo-bo/Algorithm_templates/blob/main/Manacher.cpp
{
private:
string base;
long long MOD;
int len;
vector<int> solve()
{
vector<int>R(len, 0);
int r = 0, p = 0;
for (int i = 0; i < len; ++i)
{
if (i <= r)
{
R[i] = min(R[2 * p - i], r - i);
}
while (0 <= i - R[i] - 1 && i + R[i] + 1 < len && base[i - R[i] - 1] == base[i + R[i] + 1])
{
R[i]++;
}
if (r < i + R[i])
{
r = i + R[i];
p = i;
}
}
return R;
}
public:
Manacher(string str)
{
base = "?";
for (auto& i : str)
{
base += i;
base += '?';
}
len = base.size();
MOD = LLONG_MAX;
}
Manacher(string str, long long mod) : Manacher(str)
{
MOD = mod;
}
long long palin()
{
vector<int>R = getR();
long long count = 0;
for (int i = 0; i < len; ++i)
{
count = (count + (R[i] + 1LL) / 2) % MOD;
}
return count;
}
vector<int> getR()
{
return solve();
}
};