-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuffix_array.cpp
executable file
·51 lines (48 loc) · 1.3 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
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// Description of algorithm
// log n rounds
// in each round we use sort to sort elements
// (number1,number2,suffix,)
//at the begining we have sorted by first letter
// we then
//init array to letter number
//loop:
//construct
//sort
//renumber
//TODO:calculation of LCP
vector<int> get_suffix_array(string s) {
int n=s.size();
vector<int> rank(n+1);
rank[0] = 0; //empty suffix
REP(i,n) rank[i+1]=s[i]; //suffix of length i
//NOTE: the s is no longer used so this code might be extracted to be used also in function for vector.
int sorted_by=1;
while(sorted_by<n) {
vector<pair<pair<int,int>, int> > v;
REP(i,n+1) {
v.push_back(make_pair(
make_pair(rank[i], i >= sorted_by ? rank[i-sorted_by] : 0),
i)
);
sort(v.begin(), v.end());
int x=0;
REP(i, n+1) {
if(i && v[i].first!=v[i-1].first) x++;
rank[v[i].second] = x;
}
}
sorted_by *= 2;
}
vector<int> rval(n,0); // number of suffix on ith position/ now the suffix number is index of first character
//the ranks are not from zero, if n>1
if(n>1) {
REP(i,n) {
rval[rank[i+1]-1]=n-i-1;
}
}
return rval;
}