This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 179
/
SuffixArray.cpp
79 lines (75 loc) · 2.14 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
/* source: sanfoundry.com */
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
class SuffixArray
{
private:
string *text;
int length;
int *index;
string *suffix;
public:
SuffixArray(string text)
{
this->text = new string[text.length()];
for (int i = 0; i < text.length(); i++)
{
this->text[i] = text.substr(i, 1);
}
this->length = text.length();
this->index = new int[length];
for (int i = 0; i < length; i++)
{
index[i] = i;
}
suffix = new string[length];
}
void createSuffixArray()
{
for(int index = 0; index < length; index++)
{
string text = "";
for (int text_index = index; text_index < length; text_index++)
{
text += this->text[text_index];
}
suffix[index] = text;
}
int back;
for (int iteration = 1; iteration < length; iteration++)
{
string key = suffix[iteration];
int keyindex = index[iteration];
for (back = iteration - 1; back >= 0; back--)
{
if (suffix[back].compare(key) > 0)
{
suffix[back + 1] = suffix[back];
index[back + 1] = index[back];
}
else
{
break;
}
}
suffix[back + 1] = key;
index[back + 1 ] = keyindex;
}
cout<<"SUFFIX \t INDEX"<<endl;
for (int iterate = 0; iterate < length; iterate++)
{
cout<<suffix[iterate] << "\t" << index[iterate]<<endl;
}
}
};
int main()
{
string text;
cout<<"Enter the Text String: ";
cin>>text;
SuffixArray suffixarray(text);
suffixarray.createSuffixArray();
}