-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanacher.cpp
More file actions
52 lines (41 loc) · 920 Bytes
/
manacher.cpp
File metadata and controls
52 lines (41 loc) · 920 Bytes
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
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
/*
Source: https://cp-algorithms.com/string/manacher.html
*/
vector<int> manacher_odd(string s) {
int n = s.size();
s = "$" + s + "^";
vector<int> p(n + 2);
int l = 1, r = 1;
for (int i = 1; i <= n; i++) {
p[i] = max(0, min(r - i, p[l + (r - i)]));
while (s[i - p[i]] == s[i + p[i]]) {
p[i]++;
}
if (i + p[i] > r) {
l = i - p[i], r = i + p[i];
}
}
return vector<int>(begin(p) + 1, end(p) - 1);
}
vector<int> manacher(string s) {
string t;
for (auto c : s) {
t += string("#") + c;
}
auto res = manacher_odd(t + "#");
return vector<int>(begin(res) + 1, end(res) - 1);
}
void solve() {
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
}