-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManacherAlgorithm.cpp
More file actions
47 lines (38 loc) · 808 Bytes
/
ManacherAlgorithm.cpp
File metadata and controls
47 lines (38 loc) · 808 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
class solution {
public:
std::string pre_process(std::string& s) {
int n = s.length();
if(n==0) return "^$";
std::string ret = "^";
for(int i=0;i<n;++i) {
ret += "#" + s.substr(i,1);
}
ret += "#$";
return ret;
}
std::string longest_palindrome(std::string& s) {
std::string T = pre_process(s);
const int n = T.length();
int P[n];
int C = 0, R = 0;
for(int i=1;i<n-1;++i) {
int i_mirror = 2 * C - i;
P[i] = (R > i) ? std::min(R-i, P[i_mirror]) : 0;
while(T[i+1+P[i]] == T[i-i-P[i]])
P[i] ++;
if(i + P[i] > R) {
C = i;
R = i + P[i];
}
}
int max_len = 0;
int center_index = 0;
for(int i=1;i<n-1;++i) {
if(P[i] > max_len) {
max_len = P[i];
center_index = i;
}
}
return s.substr((center_index-1-max_len)/2, max_len);
}
}