-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManachersAlgorith.cpp
More file actions
51 lines (46 loc) · 1.05 KB
/
Copy pathManachersAlgorith.cpp
File metadata and controls
51 lines (46 loc) · 1.05 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
//
// Created by timruning on 19-4-14.
//
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string manacherAlg(string &x) {
string a = "#";
for (char v:x) {
a.push_back(v);
a.append("#");
}
int pos = 0;
int maxR = 0;
vector<int> R(a.size(), 0);
for (int i = 0; i < a.size(); i++) {
R[i] = maxR > i ? min(maxR - i, R[2 * pos - i]) : 0;
while (R[i] + i < a.size() && i >= R[i] && a[i - R[i]] == a[i + R[i]]) {
R[i] += 1;
}
if (R[i] + i > maxR) {
maxR = R[i] + i;
pos = i;
}
}
int sub[] = {0, 0};
for (int i = 0; i < a.size(); i++) {
if (R[i] > sub[1]) {
sub[0] = i;
sub[1] = R[i];
}
}
string sub2 = "";
for (int i = sub[0] - sub[1] + 1; i <= sub[0] + sub[1] - 1; i++) {
if (i % 2 == 1) {
sub2 += a[i];
}
}
return sub2;
}
int main() {
string x = "waabwswfd";
string result = manacherAlg(x);
cout << result << endl;
}