-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinding Borders.cpp
More file actions
54 lines (41 loc) · 921 Bytes
/
Copy pathFinding Borders.cpp
File metadata and controls
54 lines (41 loc) · 921 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
53
54
#include <bits/stdc++.h>
using namespace std;
vector<int> prefixFunction(const string &s) {
int n = s.size();
vector<int> pi(n, 0);
for (int i = 1; i < n; i++) {
int j = pi[i - 1];
while (j > 0 && s[i] != s[j]) {
j = pi[j - 1];
}
if (s[i] == s[j]) {
j++;
}
pi[i] = j;
}
return pi;
}
vector<int> findBorders(const string &s) {
vector<int> pi = prefixFunction(s);
vector<int> borders;
int n = s.size();
int k = pi[n - 1];
while (k > 0) {
borders.push_back(k);
k = pi[k - 1];
}
reverse(borders.begin(), borders.end());
return borders;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
vector<int> borders = findBorders(s);
for (int len : borders) {
cout << len << " ";
}
return 0;
}