-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest_palindromic_substring.cc
54 lines (54 loc) · 1.69 KB
/
longest_palindromic_substring.cc
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
class Solution {
public:
// DP: O(n^2)
/*
string longestPalindrome(string s) {
string ans;
vector<vector<int>> dp(2);
int prev=0, curr=1, n = s.size();
for (int i=0;i<n;i++) {
swap(curr, prev);
dp[curr].clear();
for (int l : dp[prev]) {
if (i-l-1<0 || s[i-l-1]!=s[i]) continue;
dp[curr].push_back(l+2);
}
if (i>0 && s[i]==s[i-1]) dp[curr].push_back(2);
dp[curr].push_back(1);
if (ans.size()<dp[curr][0]) {
ans = s.substr(i-dp[curr][0]+1, dp[curr][0]);
}
}
return ans;
}
*/
// Manacher
string longestPalindrome(string s) {
int n = s.size();
// build string from "CAAB" to "^#C#A#A#B#$"
string st(n*2+1+2, '#');
vector<int> len(n*2+1+2);
st.front() = '^';
st.back() = '$';
for (int i=0;i<n;i++) st[2*i+2] = s[i];
// [i, right) half open half close iternval
int right = 0, mid = 0, longest_len = 0, longest_mid = 0;
for (int i=1; i<st.size()-1; i++) {
if (right > i) len[i] = min(right-i, len[2*mid-i]);
else len[i] = 1;
while (st[i+len[i]] == st[i-len[i]]) len[i]++;
if (len[i]+i > right) {
right = len[i]+i;
mid = i;
}
if (len[i] > longest_len) {
longest_len = len[i];
longest_mid = i;
}
}
// convert coord from st to t
int start = (longest_mid - longest_len + 1 - 1) / 2;
int length = longest_len - 1;
return s.substr(start, length);
}
};