File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < algorithm>
3+ #include < cstring>
4+
5+ using namespace std ;
6+
7+ struct suffix {
8+ int index;
9+ char *suff;
10+ };
11+
12+ int cmp (struct suffix a, struct suffix b) {
13+ return strcmp (a.suff , b.suff ) < 0 ? 1 : 0 ;
14+ }
15+
16+ int *buildSuffixArray (char *txt, int n) {
17+ struct suffix suffixes[n];
18+
19+ for (int i = 0 ;i < n;i++) {
20+ suffixes[i].index = i;
21+ suffixes[i].suff = (txt + i);
22+ }
23+
24+ sort (suffixes, suffixes + n, cmp);
25+
26+ int *suffixArr = new int [n];
27+ for (int i = 0 ;i < n;i++) {
28+ suffixArr[i] = suffixes[i].index ;
29+ }
30+
31+ return suffixArr;
32+ }
33+
34+ void printArr (int arr[], int n) {
35+ for (int i = 0 ;i < n;i++) {
36+ cout << arr[i] << " " ;
37+ }
38+ cout << endl;
39+ }
40+
41+ int main () {
42+ char txt[] = " banana" ;
43+ int n = strlen (txt);
44+ int *suffixArr = buildSuffixArray (txt, n);
45+ cout << " Following is suffix array for " << txt << endl;
46+ printArr (suffixArr, n);
47+
48+ return 0 ;
49+ }
You can’t perform that action at this time.
0 commit comments