forked from t3nsor/codebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuffix-array.cpp
69 lines (63 loc) · 1.75 KB
/
suffix-array.cpp
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
// Suffix array implementation in O(nlogn).
// Includes implementation of lcp array in O(n).
struct SuffixArray {
int n;
string T;
vector<int> SA, tempSA, RA, tempRA, L, lcp;
SuffixArray(const string &s): n(s.size() + 1), T(s),
SA(n), tempSA(n), RA(n), tempRA(n), lcp(n) {
T.push_back('$'); // text must end with $
}
inline int getRA(int i) {
return (i < n) ? RA[i] : 0;
}
void radix_sort(int k) {
int mx = max(n, 256);
L.assign(mx, 0);
for(int i = 0; i < n; ++i) L[getRA(i + k)]++;
for(int i = 0, s = 0; i < mx; ++i) {
int x = L[i];
L[i] = s;
s += x;
}
for(int i = 0; i < n; ++i) {
int& x = L[getRA(SA[i] + k)];
tempSA[x++] = SA[i];
}
for(int i = 0; i < n; ++i) SA[i] = tempSA[i];
}
// call this to build suffix array, it will be stored in SA
// first position in SA is always occupied by string "$"
void buildSA() {
for(int i = 0; i < n; ++i) {
SA[i] = i;
RA[i] = T[i];
}
for(int k = 1; k < n; k <<= 1) {
radix_sort(k);
radix_sort(0);
tempRA[SA[0]] = 0;
for(int i = 1, r = 0; i < n; ++i) {
if(getRA(SA[i - 1]) != getRA(SA[i])) r++;
else if(getRA(SA[i - 1] + k) != getRA(SA[i] + k)) r++;
tempRA[SA[i]] = r;
}
for(int i = 0; i < n; ++i) RA[i] = tempRA[i];
if(RA[SA[n - 1]] == n - 1) break;
}
}
// call this to build lcp array, it will be stored in lcp
// make sure suffix array is already built
void buildLCP() {
for(int i = 0, k = 0; i < n; ++i) {
if(RA[i] == n - 1) {
k = 0;
continue;
}
int j = SA[RA[i] + 1];
while(i + k < n && j + k < n && T[i + k] == T[j + k]) ++k;
lcp[RA[i]] = k;
if(k > 0) --k;
}
}
};