-
Notifications
You must be signed in to change notification settings - Fork 1
/
4_suffix_tree.cpp
152 lines (88 loc) · 3.1 KB
/
4_suffix_tree.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
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <stack>
using namespace std;
struct Suffix_Tree_Node {
Suffix_Tree_Node* parent{ nullptr };
map<char, Suffix_Tree_Node*> children;
int string_depth{ 0 };
int edge_start{ -1 };
int edge_end{ - 1 };
};
Suffix_Tree_Node snode_buf[400000];
int cur_buf_index = 0;
inline Suffix_Tree_Node* new_tree_node(Suffix_Tree_Node* parent, int depth, int start, int end) {
snode_buf[cur_buf_index].parent = parent;
snode_buf[cur_buf_index].string_depth = depth;
snode_buf[cur_buf_index].edge_start = start;
snode_buf[cur_buf_index].edge_end = end;
return &(snode_buf[cur_buf_index++]);
}
inline Suffix_Tree_Node* create_new_leaf(Suffix_Tree_Node* node, const string& S, int suffix) {
Suffix_Tree_Node* leaf = new_tree_node(node, S.size() - suffix, suffix + node->string_depth, S.size() - 1);
node->children[S[leaf->edge_start]] = leaf;
return leaf;
}
inline Suffix_Tree_Node* break_edge(Suffix_Tree_Node* node, const string& S, int start, int offset) {
char start_char = S[start];
char mid_char = S[start + offset];
Suffix_Tree_Node* mid_node = new_tree_node(node, node->string_depth + offset, start, start + offset - 1);
mid_node->children[mid_char] = node->children[start_char];
node->children[start_char]->parent = mid_node;
node->children[start_char]->edge_start += offset;
node->children[start_char] = mid_node;
return mid_node;
}
Suffix_Tree_Node* build_suffix_tree(const string& S, const vector<int>& order, const vector<int>& lcp_array) {
Suffix_Tree_Node* root = new_tree_node(nullptr, 0, -1 , -1);
Suffix_Tree_Node* cur_node = root;
int lcp_prev = 0;
const int N = S.size();
for(int i = 0; i < N; i++) {
auto suffix = order[i];
while (cur_node->string_depth > lcp_prev)
cur_node = cur_node->parent;
if (cur_node->string_depth == lcp_prev)
cur_node = create_new_leaf(cur_node, S, suffix);
else {
int edge_start = order[i - 1] + cur_node->string_depth;
int offset = lcp_prev - cur_node->string_depth;
Suffix_Tree_Node* mid_node = break_edge(cur_node, S, edge_start, offset);
cur_node = create_new_leaf(mid_node, S, suffix);
}
if (i < N - 1)
lcp_prev = lcp_array[i];
}
return root;
}
void preorder_print(Suffix_Tree_Node* root) {
stack<Suffix_Tree_Node*> node_stack;
for (auto iter = root->children.rbegin(); iter != root->children.rend(); iter++)
node_stack.push(iter->second);
while (!node_stack.empty()) {
Suffix_Tree_Node* node = node_stack.top();
node_stack.pop();
cout << node->edge_start << " " <<node->edge_end + 1 << endl;
for (auto it = node->children.rbegin(); it != node->children.rend(); it++)
node_stack.push(it->second);
}
}
int main() {
std::ios::sync_with_stdio(false);
string text;
cin >> text;
vector<int> suffix_array(text.size());
vector<int> lcp_array(text.size() - 1);
for (int i = 0; i < text.size(); i++) {
cin >> suffix_array[i];
}
for (int i = 0; i < text.size() - 1; i++) {
cin >> lcp_array[i];
}
auto root = build_suffix_tree(text, suffix_array, lcp_array);
cout << text << endl;
preorder_print(root);
return 0;
}