Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6b2c0ef

Browse files
authored
Suffix Array using "sort()"
1 parent dd4eba3 commit 6b2c0ef

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

‎SUFFIX_ARRAY_1.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
#define f(i,a,b) for(i=a;i<b;i++)
5+
#define rep(i,n) f(i,0,n)
6+
#define fd(i,a,b) for(i=a;i>=b;i--)
7+
#define ll long long int
8+
#define vi vector<int>
9+
#define vll vector<lli>
10+
#define pb push_back
11+
#define all(v) v.begin(),v.end()
12+
#define endl "\n"
13+
14+
15+
ll gcd(ll a, ll b){
16+
if(a == 0){
17+
return b;
18+
}
19+
if(b==0){
20+
return a;
21+
}
22+
if(a == b){
23+
return a;
24+
}
25+
if(a > b){
26+
return gcd(a-b,b);
27+
}else{
28+
return gcd(a,b-a);
29+
}
30+
}
31+
32+
bool isprime(ll n){
33+
if(n == 1) return false;
34+
ll i;
35+
for(i = 2; i*i<=n; i++){
36+
if(n%i==0) return false;
37+
}
38+
return true;
39+
}
40+
41+
void solve(){
42+
string str;
43+
cin >> str;
44+
str += "$";
45+
int i;
46+
int n = str.size();
47+
vector<int> p(n), c(n);
48+
int k = 0;
49+
// k =0;
50+
vector<pair<char,int>> a(n);
51+
rep(i,n){
52+
a[i] = {str[i],i};
53+
}
54+
sort(all(a));
55+
rep(i,n){
56+
p[i] = a[i].second;
57+
}
58+
c[p[0]] = 0;
59+
f(i,1,n){
60+
if(a[i].first == a[i-1].first){
61+
c[p[i]] = c[p[i-1]];
62+
}else{
63+
c[p[i]] = c[p[i-1]] + 1;
64+
}
65+
}
66+
while((1<<k) < n){
67+
// k -> k + 1
68+
vector<pair<pair<int,int>, int>> b(n);
69+
rep(i,n){
70+
b[i] = {{c[i],c[(i+(1<<k))%n]},i};
71+
}
72+
sort(all(b));
73+
rep(i,n){
74+
p[i] = b[i].second;
75+
}
76+
c[p[0]] = 0;
77+
f(i,1,n){
78+
if(b[i].first == b[i-1].first){
79+
c[p[i]] = c[p[i-1]];
80+
}else{
81+
c[p[i]] = c[p[i-1]]+1;
82+
}
83+
}
84+
k++;
85+
}
86+
rep(i,n){
87+
cout << p[i] << " ";
88+
}
89+
cout << endl;
90+
}
91+
92+
int main(){
93+
ios::sync_with_stdio(0);
94+
cin.tie(0);
95+
cout.tie(0);
96+
97+
int test_cases;
98+
test_cases = 1;
99+
//cin >> test_cases;
100+
while(test_cases--){
101+
solve();
102+
}
103+
return 0;
104+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /