-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanacher.cpp
More file actions
40 lines (36 loc) · 807 Bytes
/
manacher.cpp
File metadata and controls
40 lines (36 loc) · 807 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
#include<bits/stdc++.h>
using namespace std;
#define SZ(a) (int)a.size()
string s, t;
void prepare_string()
{
int i;
t = "^#";
for(i = 0; i < SZ(s); i++)
t += s[i], t += "#";
t += "$";
}
int manacher()
{
prepare_string();
int P[SZ(t)], c = 0, r = 0, i, i_mirror, n = SZ(t) - 1;
for(i = 1; i < n; i++)
{
i_mirror = (2 * c) - i;
P[i] = r > i? min(r - i, P[i_mirror]) : 0;
while(t[i + 1 + P[i]] == t[i - 1 - P[i]])
P[i]++;
if(i + P[i] > r)
{
c = i;
r = i + P[i];
}
}
return *max_element(P + 1, P + n); /// longest palindrome substrings informations can be found from array P
}
int main()
{
cin>>s;
manacher();
return 0;
}