-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuffix Array.cpp
More file actions
71 lines (63 loc) · 1.77 KB
/
Suffix Array.cpp
File metadata and controls
71 lines (63 loc) · 1.77 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
60
61
62
63
64
65
66
67
68
69
70
71
struct SuffixArray{
string s;
int n;
vector<int>p, c, lcp;
SuffixArray(string _s){
s = _s + '$';
n = (int)s.size();
build();
buildlcp();
}
void counting_sort(vector<int>&p, vector<int>&c){
vector<int>cnt(n);
for(int x : c) cnt[x]++;
vector<int>pos(n);
pos[0] = 0;
for(int i = 1; i < n; i++) pos[i] = pos[i - 1] + cnt[i - 1];
vector<int>res(n);
for(int x : p){
int i = c[x];
res[pos[i]] = x;
pos[i]++;
}
p = res;
}
void build(){
vector<pair<char, int>>a(n);
for(int i = 0; i < n; i++) a[i] = {s[i], i};
sort(a.begin(), a.end());
p.resize(n); c.resize(n);
for(int i = 0; i < n; i++) p[i] = a[i].ss;
c[p[0]] = 0;
for(int i = 1; i < n; i++){
c[p[i]] = c[p[i - 1]] + (a[i].ff != a[i - 1].ff);
}
int k = 0;
while((1LL << k) < n){
for(int i = 0; i < n; i++){
p[i] = (p[i] - (1LL << k) + n) % n;
}
counting_sort(p, c);
vector<int>res(n);
res[p[0]] = 0;
for(int i = 1; i < n; i++){
pair<int, int> now = {c[p[i]], c[(p[i] + (1LL << k)) % n]};
pair<int, int> prev = {c[p[i - 1]], c[(p[i - 1] + (1LL << k)) % n]};
res[p[i]] = res[p[i - 1]] + (prev != now);
}
c = res;
k++;
}
}
void buildlcp(){
lcp.resize(n + 1);
int k = 0;
for(int i = 0; i < n - 1; i++){
int pi = c[i];
int j = p[pi - 1];
while(s[i + k] == s[j + k]) k++;
lcp[c[i]] = k;
k = max(0LL, k - 1);
}
}
};