-
Notifications
You must be signed in to change notification settings - Fork 7
/
naive_suffix_array.cpp
48 lines (41 loc) · 1004 Bytes
/
naive_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
/*
by : Vikas Chaurasiya
*/
// Time complexity : O(N) + O(NlogN) = O(N^2(logN))
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <vector>
#include <set>
#include <climits>
#include <map>
#include <string>
using namespace std;
typedef long long int lint;
vector <int> suffix_helper(vector <pair<string, int> >&A, const string str) {
for (int i = 0; i < str.length(); ++i) {
A[i].second = i;
A[i].first = str.substr(i, str.length());
}
sort(A.begin(), A.end());
vector <int> suffix_array(str.length(), 0);
for (int i = 0; i < A.size(); ++i) {
suffix_array[i] = A[i].second;
}
return suffix_array;
}
vector <int> buildSuffix(const string str) {
vector <pair<string, int> >A(str.length(), pair<string, int> ("", 0));
return suffix_helper(A, str);
}
int main() {
string str;
cin >> str;
vector <int> suffixArray = buildSuffix(str);
for (int i = 0; i < suffixArray.size(); ++i) {
cout << suffixArray[i] << " ";
}
cout << endl;
return 0;
}