-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuffix_array_matching.cpp
220 lines (177 loc) · 4.08 KB
/
suffix_array_matching.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
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <cassert>
using namespace std;
#define ALPHABET_SIZE 5
static void print_vec(const char *s, const vector<int> &vec)
{
cout << s << ": ";
for (size_t i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
cout << endl;
}
static int letter_to_index(char c)
{
switch (c) {
case '$':
return 0;
case 'A':
return 1;
case 'C':
return 2;
case 'G':
return 3;
case 'T':
return 4;
default:
assert(0);
return 0;
}
}
static vector<int> SortCharacters(const string &s)
{
int n = s.size();
vector<int> order(n);
vector<int> cnt(ALPHABET_SIZE, 0);
for (int i = 0; i < n; i++) {
int c_i = letter_to_index(s[i]);
cnt[c_i]++;
}
for (size_t j = 1; j < cnt.size(); j++) {
cnt[j] += cnt[j-1];
}
for (int i = n - 1; i >= 0; i--) {
char c = letter_to_index(s[i]);
cnt[c]--;
order[cnt[c]] = i;
}
return order;
}
static vector<int> ComputeCharClasses(const string &s, const vector<int> &order)
{
int n = s.size();
vector<int> my_class(n);
my_class[order[0]] = 0;
for (size_t i = 1; i < n; i++) {
if (s[order[i]] != s[order[i-1]]) {
my_class[order[i]] = my_class[order[i-1]] + 1;
} else {
my_class[order[i]] = my_class[order[i-1]];
}
}
return my_class;
}
static vector<int> SortDoubled(const string &s, int L, const vector<int> &order, const vector<int> &my_class)
{
int n = s.size();
vector<int> cnt(n, 0);
vector<int> new_order(n);
for (int i = 0; i < n; i++) {
cnt[my_class[i]]++;
}
for (int j = 1; j < n; j++) {
cnt[j] += cnt[j-1];
}
for (int i = n - 1; i >= 0; i--) {
int start = (order[i] - L + n) % n;
int cl = my_class[start];
cnt[cl]--;
new_order[cnt[cl]] = start;
}
return new_order;
}
static vector<int> UpdateClasses(const vector<int> &new_order, const vector<int> &my_class, int L)
{
int n = new_order.size();
vector<int> new_class(n);
new_class[new_order[0]] = 0;
for (int i = 1; i < n; i++) {
int cur = new_order[i];
int prev = new_order[i-1];
int mid = (cur + L) % n;
int mid_prev = (prev + L) % n;
if (my_class[cur] != my_class[prev] || my_class[mid] != my_class[mid_prev]) {
new_class[cur] = new_class[prev] + 1;
} else {
new_class[cur] = new_class[prev];
}
}
return new_class;
}
static vector<int> BuildSuffixArray(const string &s)
{
vector<int> order = SortCharacters(s);
vector<int> my_class = ComputeCharClasses(s, order);
int n = s.size();
int L = 1;
while (L < n) {
order = SortDoubled(s, L, order, my_class);
my_class = UpdateClasses(order, my_class, L);
L *= 2;
}
return order;
}
static vector<int> FindOccurrences(const string &pattern, const string &text, const vector<int> &suffix_array)
{
vector<int> result;
int n = (int)text.size();
int pn = (int)pattern.size();
int min_index = 0;
int max_index = n;
int start, end;
while (min_index < max_index) {
int mid_index = (min_index + max_index)/2;
int pos = suffix_array[mid_index];
if (pattern > text.substr(pos, pn)) {
min_index = mid_index + 1;
} else {
max_index = mid_index;
}
}
start = min_index;
max_index = n;
while (min_index < max_index) {
int mid_index = (min_index + max_index)/2;
int pos = suffix_array[mid_index];
if (pattern < text.substr(pos, pn)) {
max_index = mid_index;
} else {
min_index = mid_index + 1;
}
}
end = max_index;
for (int i = start; i < end; i++) {
result.push_back(suffix_array[i]);
}
return result;
}
int main()
{
char buffer[100001];
scanf("%s", buffer);
string text = buffer;
text += '$';
vector<int> suffix_array = BuildSuffixArray(text);
int pattern_count;
scanf("%d", &pattern_count);
vector<bool> occurs(text.length(), false);
for (int pattern_index = 0; pattern_index < pattern_count; ++pattern_index) {
scanf("%s", buffer);
string pattern = buffer;
vector<int> occurrences = FindOccurrences(pattern, text, suffix_array);
for (int j = 0; j < occurrences.size(); ++j) {
occurs[occurrences[j]] = true;
}
}
for (int i = 0; i < occurs.size(); ++i) {
if (occurs[i]) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}