-
Notifications
You must be signed in to change notification settings - Fork 3
/
suffixsorting.c
201 lines (168 loc) · 5.25 KB
/
suffixsorting.c
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
// Ukkonen's suffix tree construction
// https://rosettacode.org/wiki/Talk:Ukkonen%E2%80%99s_suffix_tree_construction
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_CHAR 128
#define LIMIT 100002
struct SuffixTreeNode {
struct SuffixTreeNode *children[MAX_CHAR];
struct SuffixTreeNode *suffixLink;
int start;
int *end;
int suffixIndex;
};
typedef struct SuffixTreeNode Node;
char text[LIMIT];
Node *root = NULL;
Node *lastNewNode = NULL;
Node *activeNode = NULL;
int activeEdge = -1;
int activeLength = 0;
int remainingSuffixCount = 0;
int leafEnd = -1;
int *rootEnd = NULL;
int *splitEnd = NULL;
int size = -1;
Node *newNode(int start, int *end) {
Node *node = (Node*) malloc(sizeof(Node));
for (int i = 0; i < MAX_CHAR; i++)
node->children[i] = NULL;
node->suffixLink = root;
node->start = start;
node->end = end;
node->suffixIndex = -1;
return node;
}
int edgeLength(Node *n) {
if (n == root)
return 0;
return *(n->end) - (n->start) + 1;
}
int walkDown(Node *currNode) {
if (activeLength >= edgeLength(currNode)) {
activeEdge += edgeLength(currNode);
activeLength -= edgeLength(currNode);
activeNode = currNode;
return 1;
}
return 0;
}
void extendSuffixTree(int pos) {
leafEnd = pos;
remainingSuffixCount++;
lastNewNode = NULL;
while (remainingSuffixCount > 0) {
if (activeLength == 0)
activeEdge = pos;
if (activeNode->children[text[activeEdge]] == NULL) {
activeNode->children[text[activeEdge]] = newNode(pos, &leafEnd);
if (lastNewNode != NULL) {
lastNewNode->suffixLink = activeNode;
lastNewNode = NULL;
}
} else {
Node *next = activeNode->children[text[activeEdge]];
if (walkDown(next))
continue;
if (text[next->start + activeLength] == text[pos]) {
if(lastNewNode != NULL && activeNode != root) {
lastNewNode->suffixLink = activeNode;
lastNewNode = NULL;
}
activeLength++;
break;
}
splitEnd = (int*) malloc(sizeof(int));
*splitEnd = next->start + activeLength - 1;
Node *split = newNode(next->start, splitEnd);
activeNode->children[text[activeEdge]] = split;
split->children[text[pos]] = newNode(pos, &leafEnd);
next->start += activeLength;
split->children[text[next->start]] = next;
if (lastNewNode != NULL)
lastNewNode->suffixLink = split;
lastNewNode = split;
}
remainingSuffixCount--;
if (activeNode == root && activeLength > 0) {
activeLength--;
activeEdge = pos - remainingSuffixCount + 1;
} else if (activeNode != root) {
activeNode = activeNode->suffixLink;
}
}
}
void setSuffixIndexByDFS(Node *n, int labelHeight) {
if (n == NULL)
return;
int leaf = 1;
for (int i = 0; i < MAX_CHAR; i++) {
if (n->children[i] != NULL) {
leaf = 0;
setSuffixIndexByDFS(n->children[i], labelHeight + edgeLength(n->children[i]));
}
}
if (leaf == 1) {
n->suffixIndex = size - labelHeight;
}
}
void freeSuffixTreeByPostOrder(Node *n) {
if (n == NULL)
return;
for (int i = 0; i < MAX_CHAR; i++) {
if (n->children[i] != NULL)
freeSuffixTreeByPostOrder(n->children[i]);
}
if (n->suffixIndex == -1)
free(n->end);
free(n);
}
void buildSuffixTree() {
size = strlen(text);
rootEnd = (int*) malloc(sizeof(int));
*rootEnd = - 1;
root = newNode(-1, rootEnd);
activeNode = root;
for (int i = 0; i < size; i++)
extendSuffixTree(i);
int labelHeight = 0;
setSuffixIndexByDFS(root, labelHeight);
}
void doTraversal(Node *n, int suffixArray[], int *idx) {
if (n == NULL)
return;
int i = 0;
if (n->suffixIndex == -1) {
for (i = 0; i < MAX_CHAR; i++)
if (n->children[i] != NULL)
doTraversal(n->children[i], suffixArray, idx);
} else if (n->suffixIndex > -1 && n->suffixIndex < size)
suffixArray[(*idx)++] = n->suffixIndex;
}
void buildSuffixArray(int suffixArray[]) {
for (int i = 0; i < size; i++)
suffixArray[i] = -1;
int idx = 0;
doTraversal(root, suffixArray, &idx);
}
int main(int argc, char *argv[]) {
int t, k;
while (scanf("%[^\n]%*c", text) == 1) {
strcat(text, ""); // char 1
buildSuffixTree();
size--;
int suffixArray[strlen(text)];
buildSuffixArray(suffixArray);
if (scanf("%d", &t) == 1)
t--;
while (t--) {
if (scanf("%d", &k) == 1)
printf("%d ", suffixArray[k]);
}
if (scanf("%d\n", &k) == 1)
printf("%d\n", suffixArray[k]);
freeSuffixTreeByPostOrder(root);
}
return 0;
}