-
Notifications
You must be signed in to change notification settings - Fork 3
/
manacher.cpp
46 lines (36 loc) · 1022 Bytes
/
manacher.cpp
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
/**
Manacher's Algorithm - Finding all sub-palindromes in O(N)
Problem: https://leetcode.com/problems/longest-palindromic-substring
*/
vector<int> manacher_odd(string &s) {
int n = s.size();
s = "^" + s + "$";
vector<int> pal(n + 2);
int l = 1, r = 1;
for (int i = 1; i <= n; ++i) {
int mirror = pal[l + (r - i)];
pal[i] = max(0, min(r - i, mirror));
while (s[i - pal[i]] == s[i + pal[i]]) {
++pal[i];
}
if (i + pal[i] > r) {
l = i - pal[i], r = i + pal[i];
}
}
return vector<int>(begin(pal) + 1, end(pal) - 1);
}
vector<int> manacher(const string &s) {
string t = "#";
for (char c : s)
t += c + string("#");
vector<int> res = manacher_odd(t);
return vector<int>(begin(res) + 1, end(res) - 1);
}
void Solve() {
string s = "ababa";
vector<int> v = manacher(s);
int mx = *max_element(begin(v), end(v));
int maxIdx = find(begin(v), end(v), mx) - begin(v);
maxIdx -= (maxIdx + 1) / 2;
return s.substr(maxIdx - (mx / 2) + 1, mx - 1);
}