-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsuffix array 1.cpp
143 lines (121 loc) · 3.39 KB
/
suffix array 1.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include<bits/stdc++.h>
using namespace std;
int *finalsuffixans;
struct suffixArray{
int index; // to keep track of original index of suffix
int tuple[2]; //maintain "rank" and "next rank"
};
//comparator for comparing two tuple
bool comfun(struct suffixArray obj1, struct suffixArray obj2)
{
if(obj1.tuple[0]==obj2.tuple[0])
{
if(obj1.tuple[1]<obj2.tuple[1])
return true;
else
return false;
}
else if(obj1.tuple[0]<obj2.tuple[0])
{
return true;
}
else
{
return false;
}
}
//create suffix array of given string
void createsufficArray(string str)
{
int size1=str.length();
struct suffixArray suffobj[size1];
int suffInd[size1];
// Intitialisation of tuple w.r.t first two character
for(int i=0;i<size1;i++)
{
suffobj[i].index=i;
suffobj[i].tuple[0]=str[i]-'0';
if(i<size1-1)
{
suffobj[i].tuple[1]=str[i+1]-'0';
}
else{
suffobj[i].tuple[1]=-1;
}
}
//sort according to tuple;
sort(suffobj,suffobj+size1, comfun);
for(int j=2;j<size1;j=j*2)
{
//initialised rank and index of first suffix;
int curRank=0;
int preRank=suffobj[0].tuple[0];
suffobj[0].tuple[0]=curRank;
suffInd[suffobj[0].index]=0;
//compute new rank and index for all suffix
for(int k=1;k<size1;k++)
{
int curtuple1=suffobj[k].tuple[0];
int curtuple2=suffobj[k].tuple[1];
int pretuple2=suffobj[k-1].tuple[1];
// if cur tuple/rank is same as previous tuple/rank then assigned same rank to suffix
if(curtuple1==preRank && curtuple2==pretuple2)
{
preRank=suffobj[k].tuple[0];
suffobj[k].tuple[0]= curRank;
}
//else increment rank by 1 and then assigned to suffix
else{
preRank=suffobj[k].tuple[0];
suffobj[k].tuple[0]=++curRank;
}
suffInd[suffobj[k].index]=k;
}
//compute "next rank/tuple" for all suffix
for (int i=0;i<size1;i++)
{
int nextind = suffobj[i].index + j;
if(nextind < size1){
suffobj[i].tuple[1]=suffobj[suffInd[nextind]].tuple[0];
}
else{
suffobj[i].tuple[1]=-1;
}
}
//sort all suffix
sort(suffobj, suffobj+size1, comfun);
}
// Store all indexes of sorted suffixes in the suffix array
finalsuffixans = new int[size1];
for (int i=0;i<size1;i++)
{
finalsuffixans[i] = suffobj[i].index;
}
}
int main()
{
string s;
cin>>s;
string doublestr= s+s;
long int len=doublestr.length();
//create suffix array on double_string
createsufficArray(doublestr);
// cout<<"Suffix Array for given string : ";
// for(int i=0;i<len;i++)
// {
// cout<< finalsuffixans[i]<<" ";
// }
//cover case CABA -> ABAC
long int smallindex;
for(long int i=0;i<len;i++)
{
if(finalsuffixans[i]<(s.length()))
{
smallindex=finalsuffixans[i];
break;
}
}
string lexsmallstr = doublestr.substr(smallindex,s.length());
cout<<"\nMinimum Lexicographic rotation : "<<lexsmallstr<<endl;
return 0;
}