-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstring_manacher.cpp
More file actions
43 lines (42 loc) 路 1.18 KB
/
Copy pathstring_manacher.cpp
File metadata and controls
43 lines (42 loc) 路 1.18 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
template <typename T> vector<int> manacher(const T &s) {
int n = (int)s.size();
if (n == 0)
return vector<int>();
vector<int> res(2 * n - 1, 0);
int l = -1, r = -1;
for (int z = 0; z < 2 * n - 1; z++) {
int i = (z + 1) >> 1;
int j = z >> 1;
int p = (i >= r ? 0 : min(r - i, res[2 * (l + r) - z]));
while (j + p + 1 < n && i - p - 1 >= 0) {
if (!(s[j + p + 1] == s[i - p - 1]))
break;
p++;
}
if (j + p > r) {
l = i - p;
r = j + p;
}
res[z] = p;
}
// Time Complexity: O(N), Space Complexity: O(N)
return res;
}
template <typename T> vector<string> palindromes(const T &txt) {
vector<int> res = manacher(txt);
int n = (int)txt.size();
vector<string> answer;
for (int z = 0; z < 2 * n - 1; ++z) {
int i = (z + 1) / 2;
int j = z / 2;
if (i > j && res[z] == 0)
continue;
int from = i - res[z];
int to = j + res[z];
string pal = "";
for (int i = from; i <= to; ++i)
pal.push_back(txt[i]);
answer.push_back(pal);
}
return answer;
}