-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ukkonen.cpp
229 lines (210 loc) · 4.69 KB
/
Ukkonen.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
class suffix_tree
{
private:
class node {
public:
node * parent;
node * suffixLink;
int edge_start;
int edge_end;
int parent_depth;
std::unordered_map<char, node *> children;
public:
node():
parent(NULL),suffixLink(NULL),edge_start(0),edge_end(0),parent_depth(0)
{
children = std::unordered_map<char, node *>();
}
bool edge__(int position)
{
return parent_depth <= position && position < depth();
}
int edge_len()
{
return edge_end - edge_start;
}
int depth()
{
return parent_depth + edge_len();
}
void link(node * child, int start, int end, const std::string &s)
{
children[s[start]] = child;
child->parent = this;
child->parent_depth = this->depth();
child->edge_start = start;
child->edge_end = end;
}
bool is_leaf()
{
return children.size() == 0;
}
};
private:
std::vector<node> child_nodes;
node * head, * req_suffix;
int current_node;
std::string s;
int length;
public:
suffix_tree()
{
}
suffix_tree(const std::string &str):
length(str.length()),s(str)
{
build();
}
private:
node * create_node()
{
child_nodes[current_node] = node();
return &child_nodes[current_node++];
}
node * go_down(node * c, int j, int i)
{
int k = j + c->depth();
if (i - j + 1 > 0)
{
while (!c->edge__(i - j))
{
c = c->children[s[k]];
k += c->edge_len();
}
}
return c;
}
void suffix_links(node * current)
{
if (req_suffix != NULL)
{
req_suffix->suffixLink = current;
}
req_suffix = NULL;
}
bool check(node *c,const std::string &instr)
{
static int index=0;
if(c==NULL)
return false;
for(int i = c->edge_start;i<c->edge_end&&index<instr.size();i++)
{
if(s[i]==instr[index])
index++;
else
break;
}
if(index==instr.size())
return true;
std::cout<<index<<" "<<instr.size()<<std::endl;
std::cout<<c->edge_start<<" "<<c->edge_end<<std::endl;
std::cout<<instr[index]<<std::endl;
if(c->children.find(instr[index])!=c->children.end())
{
index++;
return check(c->children[instr[index]],instr);
}
return false;
}
void build()
{
if (child_nodes.size() < 2*length + 10)
{
child_nodes.resize(2*length + 10);
}
current_node = 0;
head = create_node();
node * c = create_node();
req_suffix = NULL;
head->link(c, 0, length, s);
bool walk_down = true;
for (int i=0, j=1; i<length-1; ++i)
{
char temp_char = s[i+1];
while (j <= i + 1)
{
if (walk_down)
{
if (c->suffixLink == NULL && c->parent != NULL) c = c->parent;
c = (c->suffixLink == NULL ? head : c->suffixLink);
c = go_down(c, j, i);
}
walk_down = true;
int m = i - j + 1;
if (m == c->depth())
{
suffix_links(c);
if (c->children.count(temp_char) > 0)
{
c = c->children[temp_char];
walk_down = false;
break;
}
else
{
node * leaf = create_node();
c->link(leaf, i+1, length, s);
}
}
else{
int temp = c->edge_start + m - c->parent_depth;
if (s[temp] == temp_char) // rule 1 or 3
{
suffix_links(c);
if (!c->is_leaf() || j != c->edge_start - c->parent_depth)
{
// rule 3
walk_down = false;
break;
}
}
else
{
node * split = create_node();
c->parent->link(split, c->edge_start, temp, s);
split->link(c, temp, c->edge_end, s);
split->link(create_node(), i+1, length, s);
suffix_links(split);
if (split->depth() == 1)
{
split->suffixLink = head;
}
else
{
req_suffix = split;
}
c = split;
}
}
j++;
}
}
}
public:
void check(const std::string str)
{
if(check(head,str))
std::cout<<"found";
else
std::cout<<"not found";
}
};
int main(){
/* std::ifstream in("AesopTales.txt");
std::stringstream buffer;
buffer << in.rdbuf();
std::string Tales(buffer.str());
*/
std::string s ="helloworld";
suffix_tree t(s+"$");
//cout<<Tales;
t.check("owor");
return 0;
}