-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSuffixTree.cpp
193 lines (184 loc) · 6.16 KB
/
SuffixTree.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* C++ Program to Implement Suffix Tree
*/
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
typedef unsigned char byte;
/* SuffixNode */
class SuffixNode
{
public:
int depth, begin, end;
SuffixNode **children;
SuffixNode *parent, *suffixLink;
/* Constructor */
SuffixNode(int begin, int end, int depth, SuffixNode *parent)
{
children = new SuffixNode* [38];
this->begin = begin;
this->end = end;
this->parent = parent;
this->depth = depth;
}
bool contains(int d)
{
return depth <= d && d < depth + (end - begin);
}
};
string alphabet;
int alphabetSize;
int lcsLength;
int lcsBeginIndex;
/* Class SuffixTree */
class SuffixTree
{
public:
/* Funtion to build suffix tree for given text */
SuffixNode *buildSuffixTree(string s)
{
int n = s.length();
int *a = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = alphabet.find(s.at(i));
}
SuffixNode *root = new SuffixNode(0, 0, 0, NULL);
SuffixNode *cn = root;
root->suffixLink = root;
SuffixNode *needsSuffixLink = NULL;
int lastRule = 0;
int j = 0;
for (int i = -1; i < n - 1; i++)
{
int cur = a[i + 1];
for (; j <= i + 1; j++)
{
int curDepth = i + 1 - j;
if (lastRule != 3)
{
if (cn->suffixLink != NULL)
cn = cn->suffixLink;
else
cn = cn->parent->suffixLink;
int k = j + cn->depth;
while (curDepth > 0 && !cn->contains(curDepth - 1))
{
k += cn->end - cn->begin;
cn = cn->children[a[k]];
}
}
if (!cn->contains(curDepth))
{
if (needsSuffixLink != NULL)
{
needsSuffixLink->suffixLink = cn;
needsSuffixLink = NULL;
}
if (cn->children[cur] == NULL)
{
cn->children[cur] = new SuffixNode(i + 1, n, curDepth, cn);
lastRule = 2;
}
else
{ /* already exists */
cn = cn->children[cur];
lastRule = 3;
break;
}
}
else
{
int end = cn->begin + curDepth - cn->depth;
if (a[end] != cur)
{
SuffixNode *newn = new SuffixNode(cn->begin, end, cn->depth, cn->parent);
newn->children[cur] = new SuffixNode(i + 1, n, curDepth, newn);
newn->children[a[end]] = cn;
cn->parent->children[a[cn->begin]] = newn;
if (needsSuffixLink != NULL)
needsSuffixLink->suffixLink = newn;
cn->begin = end;
cn->depth = curDepth;
cn->parent = newn;
cn = needsSuffixLink = newn;
lastRule = 2;
}
else if (cn->end != n || (cn->begin - cn->depth) < j)
{
lastRule = 3;
break;
}
else
lastRule = 1;
}
}
}
root->suffixLink = NULL;
return root;
}
/* Funtion to find longest common substring */
int findLCS(SuffixNode *node, int i1, int i2)
{
if (node->begin <= i1 && i1 < node->end)
return 1;
if (node->begin <= i2 && i2 < node->end)
return 2;
int mask = 0;
for (int f = 0; f < alphabetSize; f++)
if (node->children[f] != NULL)
{
mask |= findLCS(node->children[f], i1, i2);
}
if (mask == 3)
{
int curLength = node->depth + node->end - node->begin;
if (lcsLength < curLength)
{
lcsLength = curLength;
lcsBeginIndex = node->begin;
}
}
return mask;
}
/* Funtion to find longest common substring */
void findLCS(string s1, string s2)
{
string x = "-";
string y = "#";
string ns1 = s1;
string ns2 = s2;
string s = s1.append(x.append(s2.append(y)));
SuffixNode *root = buildSuffixTree(s);
lcsLength = 0;
lcsBeginIndex = 0;
findLCS(root, ns1.length(), ns1.length() + ns2.length() + 1);
bool chk = lcsLength > 0;
if (chk)
{
cout<<"\nLongest substring is "<<s.substr(lcsBeginIndex , lcsLength);
cout<<endl;
}
else
{
cout<<"No substring found"<<endl;
}
}
};
/* Main */
int main()
{
alphabet = "abcdefghijklmnopqrstuvwxyz1234567890-#";
alphabetSize = alphabet.length();
string s1,s2;
cout<<"Finding longest common substring using suffix trees\n";
cout<<"Enter 1st String: ";
cin>>s1;
cout<<"Enter 2nd String: ";
cin>>s2;
SuffixTree st;
st.findLCS(s1, s2);
return 0;
}