-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.cpp
More file actions
51 lines (48 loc) · 1.07 KB
/
Copy path5.cpp
File metadata and controls
51 lines (48 loc) · 1.07 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
48
49
50
51
#include "common.h"
using namespace std;
class Solution {
public:
string longestPalindrome(string s) {
string t = "#";
for (auto c: s) {
t += c;
t += "#";
}
int n = t.size();
int start = 0, len = 0;
vector<int> lens(n);
int mid = 0, right = -1;
for (int i = 0; i < n; ++i) {
int cur_len;
if (right > i) {
int mirror = mid * 2 - i;
int skip = min(lens[mirror], right - i);
cur_len = expand(t, i - skip, i + skip);
} else {
cur_len = expand(t, i, i);
}
lens[i] = cur_len;
if (i + cur_len > right) {
mid = i;
right = i + cur_len;
}
if (cur_len > len) {
start = (i - cur_len + 1) / 2;
len = cur_len;
}
}
return s.substr(start, len);
}
int expand(string& s, int left, int right) {
while (left >= 0 && right < s.size() && s[left] == s[right]) {
left -= 1;
right += 1;
}
return (right - left - 2) / 2;
}
};
int main() {
Solution s;
cout << s.longestPalindrome("cbbd") << endl;
return 0;
}