-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManacher.cpp
More file actions
59 lines (55 loc) · 1.23 KB
/
Copy pathManacher.cpp
File metadata and controls
59 lines (55 loc) · 1.23 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
52
53
54
55
56
57
58
59
/**
* Author: Kevin Li
* Lang: C++
* Description: Manacher Palindrome search algorithm
*/
#include <iostream>
#include <vector>
using namespace std;
struct manacher {
string S;
manacher (string _s) : S(_s) {}
vector<int> res;
void driver() {
res.clear();
string s = "@";
for (char c : S) {
s += c;
s += "#";
}
s[s.length()-1] = '&';
res = vector<int>(s.length()-1);
int l,h; l = h = 0;
for (int i = 1; i <= s.length()-1; i++) {
if (i != 1) {
res[i] = min(h-i,res[h-i+l]);
}
while (s[i-res[i]-1] == s[i+res[i]+1]) {
res[i]++;
}
if (i + res[i] > h) {
l = i - res[i];
h = i + res[i];
}
}
res.erase(res.begin());
for (int i = 0; i < res.size(); i++) {
if ((i&1) == (res[i]&1)) {
res[i]++;
}
}
}
void print() {
for (int i = 0; i < res.size(); i++) {
cout << res[i] << " ";
}
cout << endl;
}
};
string s;
int main() {
cin >> s;
manacher M = manacher(s);
M.driver();
M.print();
}