forked from TECHOUS/DSKaKhel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuffixArray.cpp
More file actions
69 lines (63 loc) · 1.41 KB
/
suffixArray.cpp
File metadata and controls
69 lines (63 loc) · 1.41 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
#include<bits/stdc++.h>
typedef long long int ll;
#define vi vector<ll>
#define pi pair<ll,ll>
#define pb push_back
#define F first
#define S second
#define B begin()
#define E end()
#define mp make_pair
using namespace std;
suffixArray(string str, int n){
str+='$';
n+=1;
vector<pair<char,int> >index;
for(int i =0; i < n; i++){
index.pb(mp(str[i],i));
}
sort(index.B,index.E);
vector<int> p(n),c(n);
int idx = 0;
for(int i = 0; i < n; i++) p[i] = index[i].S;
c[p[0]] = 0;
for(int i = 1; i < n; i++){
if(index[i].F == index[i-1].F){
c[p[i]] = c[p[i-1]];
}
else{
c[p[i]] = c[p[i-1]]+1;
}
}
int k = 1, len = 1, diff = 0;
while(len < n && diff< n){
vector<pair<pair<int,int>,int> > aux(n);
for(int i = 0; i < n; i++){
aux[i] = {{c[i],c[(i+len)%n]},i};
}
sort(aux.B,aux.E);
for(int i = 0; i < n; i++) p[i] = aux[i].S;
c[p[0]] = 0;
for(int i = 1; i < n; i++){
if(aux[i].F == aux[i-1].F){
c[p[i]] = c[p[i-1]];
}
else{
c[p[i]] = c[p[i-1]]+1;
}
}
len*=2;
}
for(int i = 0; i < n; i++){
cout<<p[i]<<" ";
}
cout<<"\n";
}
int main ()
{
string str;
cin>>str;
int n = str.length();
suffixArray(str,n);
return 0;
}