-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
31 lines (28 loc) · 829 Bytes
/
Copy pathsolution.cpp
File metadata and controls
31 lines (28 loc) · 829 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
// Stepsort · Kasai's Algorithm
// Category: String
// Animated walkthrough: https://stepsort.prakashraj.me/algorithm/kasai-algorithm
#include <bits/stdc++.h>
using namespace std;
vector<int> kasai(const string& s, const vector<int>& sa) {
int n = s.size();
vector<int> rank(n), lcp(n, 0);
for (int i = 0; i < n; i++) rank[sa[i]] = i;
int h = 0;
for (int i = 0; i < n; i++) {
if (rank[i] > 0) {
int j = sa[rank[i] - 1];
while (i + h < n && j + h < n && s[i + h] == s[j + h]) h++;
lcp[rank[i]] = h;
if (h > 0) h--;
} else {
h = 0;
}
}
return lcp;
}
int main() {
string s = "banana";
vector<int> sa = {5, 3, 1, 0, 4, 2};
for (int v : kasai(s, sa)) cout << v << " ";
cout << endl; // 0 1 3 0 0 2
}