forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0005.cpp
More file actions
47 lines (41 loc) · 1.13 KB
/
0005.cpp
File metadata and controls
47 lines (41 loc) · 1.13 KB
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:
string longestPalindrome(string s) {
int len = s.length() * 2 + 3;
// Manacher's Algorithm
string T(len, '#');
T[0] = '$';
T[len - 1] = '@';
for (int i = 2; i < len - 2; i += 2)
T[i] = s[i / 2 - 1];
int center = 1;
int right = 1;
vector<int> P(len, 0);
for (int i = 1; i < len - 1; i++) {
int mirr = 2 * center - i;
if (i < right) P[i] = min(P[mirr], right - i);
while (T[i + P[i] + 1] == T[i - P[i] - 1])
P[i]++;
if (i + P[i] > right) {
center = i;
right = i + P[i];
}
}
// find max and the center;
int max = 0;
int c = 0;
for (int i = 0; i < len; i++)
if (P[i] > max) {
max = P[i];
c = i;
}
// omit '#' and get the string desired
string ans(max, '#');
int i = 0;
for (int j = c - max + 1; j < c + max; j += 2) {
ans[i] = T[j];
i++;
}
return ans;
}
};