-
Notifications
You must be signed in to change notification settings - Fork 0
/
suffixArray.cpp
99 lines (90 loc) · 2.33 KB
/
suffixArray.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
#include <bits/stdc++.h>
#define ll long long
using namespace std;
pair<ll, pair<ll, ll>> *suffixarray;
ll inputlen;
string input;
ll comp(pair<ll, pair<ll, ll>> a, pair<ll, pair<ll, ll>> b)
{
ll f = a.second.first;
ll n = a.second.second;
ll f1 = b.second.first;
ll n1 = b.second.second;
if (f < f1)
return 1;
else if (f > f1)
return 0;
else
{
if (n <= n1)
return 1;
else
return 0;
}
}
void suffixSort()
{
ll i, j;
for (i = 0; i < inputlen; i++)
{
suffixarray[i].first = i;
suffixarray[i].second.first = input[i] - '0';
if (i + 1 < inputlen)
{
suffixarray[i].second.second = input[i + 1] - '0';
}
else
suffixarray[i].second.second = -1;
}
sort(suffixarray, suffixarray + inputlen, comp);
ll index[inputlen];
for (i = 2; i < inputlen; i = i * 2)
{
ll current = 0;
ll previous = suffixarray[0].second.first;
suffixarray[0].second.first = current;
index[suffixarray[0].first] = 0;
for (j = 1; j < inputlen; j++)
{
ll prevnext = suffixarray[j].second.second;
ll prevprev = suffixarray[j - 1].second.second;
ll prev = suffixarray[j].second.first;
if (prevnext == prevprev && previous == prev)
{
previous = prev;
suffixarray[j].second.first = current;
}
else
{
previous = prev;
suffixarray[j].second.first = ++current;
}
index[suffixarray[j].first] = j;
}
for (j = 0; j < inputlen; j++)
{
ll child_1 = suffixarray[j].first + i;
if (child_1 < inputlen)
{
ll in = index[child_1];
suffixarray[j].second.second = suffixarray[in].second.first;
}
else
{
suffixarray[j].second.second = -1;
}
}
sort(suffixarray, suffixarray + inputlen, comp);
}
}
int main()
{
ll i;
cin >> input;
inputlen = input.size();
suffixarray = new pair<ll, pair<ll, ll>>[inputlen];
suffixSort();
for (i = 0; i < inputlen; i++)
cout << suffixarray[i].first << " ";
return 0;
}