Repository navigation
Expand file tree
/
Copy pathAhoCorasickAutomation.cpp
More file actions
373 lines (312 loc) · 10 KB
/
Copy pathAhoCorasickAutomation.cpp
File metadata and controls
373 lines (312 loc) · 10 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
template <typename Container>
class AlphaBet {
public:
template <bool is_const>
class Iterator;
using iterator = Iterator<false>; // NOLINT
using const_iterator = Iterator<true>; // NOLINT
using reverse_iterator = std::reverse_iterator<iterator>; // NOLINT
using container_allocator = typename Container::allocator_type; // NOLINT
using value_type = typename Container::value_type; // NOLINT
using container = Container; // NOLINT
AlphaBet() = default;
explicit AlphaBet(const Container& container)
: container_(container), alphabet_power_(container.size()) {}
AlphaBet(const AlphaBet<Container>& alpha_bet)
: container_(alpha_bet.container_),
alphabet_power_(alpha_bet.alphabet_power_) {}
AlphaBet(AlphaBet<Container>&& alpha_bet) noexcept
: container_(std::move(alpha_bet.container_)),
alphabet_power_(alpha_bet.alphabet_power_) {}
AlphaBet& operator=(const AlphaBet<Container>& alpha_bet) {
container_ = alpha_bet.container_;
alphabet_power_ = alpha_bet.alphabet_power_;
return *this;
}
AlphaBet& operator=(AlphaBet<Container>&& alpha_bet) noexcept {
if (&alpha_bet != this) {
container_ = std::move(alpha_bet.container_);
alphabet_power_ = alpha_bet.alphabet_power_;
alpha_bet.alphabet_power_ = alpha_bet.container_.size();
}
return *this;
}
value_type GetSymbol(size_t pos) { return container_[pos]; }
size_t GetPosOfSymbol(const value_type& value) {
for (size_t pos = 0; pos < container_.size(); ++pos) {
if (container_[pos] == value) {
return pos;
}
}
return container_.size();
}
size_t Power() const { return alphabet_power_; }
iterator begin() { return iterator(container_.begin()); } // NOLINT
const_iterator begin() const { // NOLINT
return const_iterator(container_.begin());
}
reverse_iterator rbegin() { return reverse_iterator(begin()); } // NOLINT
iterator end() { return iterator(container_.end()); } // NOLINT
const_iterator end() const { // NOLINT
return const_iterator(container_.end());
}
reverse_iterator rend() { return reverse_iterator(end()); } // NOLINT
private:
Container container_;
size_t alphabet_power_;
};
template <typename Container>
template <bool is_const>
class AlphaBet<Container>::Iterator {
public:
using value_type = // NOLINT
std::conditional_t<is_const, const typename Container::value_type,
typename Container::value_type>;
using pointer = value_type*; // NOLINT
using reference = value_type&; // NOLINT
using iterator_category = // NOLINT
typename Container::iterator::iterator_category;
using difference_type = // NOLINT
typename Container::iterator::difference_type;
Iterator(const typename Container::iterator& iterator)
: iterator_(iterator) {}
Iterator(const Iterator<is_const>& iterator)
: iterator_(iterator.iterator_) {}
Iterator(Iterator<is_const>&& iterator)
: iterator_(std::move(iterator.iterator_)) {}
Iterator& operator=(const Iterator<is_const>& iterator) {
iterator_ = iterator.iterator_;
return *this;
}
Iterator& operator=(Iterator<is_const>&& iterator) noexcept {
if (&iterator != this) {
iterator_ = std::move(iterator.iterator_);
}
return *this;
}
~Iterator() = default;
operator const_iterator() {
return AlphaBet<Container>::Iterator<true>(iterator_);
};
Iterator& operator++() {
++iterator_;
return *this;
}
Iterator operator++(int) {
auto copy = *this;
++iterator_;
return copy;
}
Iterator& operator--() {
--iterator_;
return *this;
}
Iterator operator--(int) {
auto copy = *this;
--iterator_;
return copy;
}
Iterator& operator+=(difference_type diff) {
iterator_ += diff;
return *this;
}
Iterator& operator-=(difference_type diff) {
iterator_ -= diff;
return *this;
}
Iterator operator+(difference_type diff) const {
auto copy = *this;
copy += diff;
return copy;
}
Iterator operator-(difference_type diff) const {
auto copy = *this;
copy -= diff;
return copy;
}
difference_type operator-(const Iterator<is_const>& other) const {
return iterator_ - other.iterator_;
}
bool operator<(const Iterator<is_const>& other) const {
return iterator_ < other.iterator_;
}
template <bool is_const_other>
bool operator==(const Iterator<is_const_other>& other) const {
return iterator_ == other.iterator_;
}
template <bool is_const_other>
bool operator!=(const Iterator<is_const_other>& other) const {
return !(*this == other);
}
bool operator<=(const Iterator<is_const>& other) {
return (*this == other || *this < other);
}
bool operator>(const Iterator<is_const>& other) { return !(*this <= other); }
bool operator>=(const Iterator<is_const>& other) { return !(*this < other); }
pointer operator->() const { return &(*iterator_); }
reference operator*() const { return *iterator_; }
private:
typename Container::iterator iterator_;
};
template <typename AlphaBet>
class AhoCorasickAutomation {
public:
using value_type = typename AlphaBet::value_type; // NOLINT
explicit AhoCorasickAutomation(AlphaBet alpha_bet)
: alpha_bet_(std::move(alpha_bet)) {}
void Add(const std::vector<value_type>& word) {
if (trie_.empty()) {
trie_.emplace_back(Node{alpha_bet_.Power()});
}
size_t node = 0;
for (const auto& symbol : word) {
if (trie_[node].to[alpha_bet_.GetPosOfSymbol(symbol)] == -1) {
trie_[node].to[alpha_bet_.GetPosOfSymbol(symbol)] =
static_cast<ssize_t>(trie_.size());
trie_.emplace_back(Node{alpha_bet_.Power()});
++(trie_[trie_.size() - 1].len = trie_[node].len);
}
node = trie_[node].to[alpha_bet_.GetPosOfSymbol(symbol)];
}
trie_[node].terminal = true;
trie_[node].dict_pos.push_back(dict_size_);
++dict_size_;
}
void Init() {
Resize();
std::queue<size_t> queue;
queue.push(0);
PreCalculateGoForRoot();
while (!queue.empty()) {
size_t node = queue.front();
queue.pop();
CalculateCompressedLink(node);
for (size_t edge = 0; edge < alpha_bet_.Power(); ++edge) {
ssize_t next = trie_[node].to[edge];
if (next == -1) {
continue;
}
link_[next] = node > 0 ? go_[link_[node]][edge] : 0;
for (size_t next_symbol = 0; next_symbol < alpha_bet_.Power();
++next_symbol) {
if (trie_[next].to[next_symbol] != -1) {
go_[next][next_symbol] = trie_[next].to[next_symbol];
} else {
go_[next][next_symbol] = go_[link_[next]][next_symbol];
}
}
queue.push(next);
}
}
}
auto Calculate(const std::vector<value_type>& text) {
size_t node = 0;
for (size_t pos = 0; pos < text.size(); ++pos) {
node = go_[node][alpha_bet_.GetPosOfSymbol(text[pos])];
CalculateAnswerForTerminals(node, pos);
}
return dict_pos_;
}
void Reset() { dict_pos_.resize(dict_size_, std::vector<size_t>(1)); }
private:
struct Node {
size_t alphabet_power;
std::vector<ssize_t> to = std::vector<ssize_t>(alphabet_power, -1);
bool terminal = false;
size_t len = 0;
std::vector<size_t> dict_pos = std::vector<size_t>();
};
void Resize() {
dict_pos_.resize(dict_size_, std::vector<size_t>(1));
link_.resize(trie_.size());
compressed_link_.resize(trie_.size());
go_.resize(trie_.size(), std::vector<ssize_t>(alpha_bet_.Power()));
}
void PreCalculateGoForRoot() {
const auto& root = trie_[0];
for (size_t symbol = 0; symbol < alpha_bet_.Power(); ++symbol) {
if (root.to[symbol] != -1) {
go_[0][symbol] = root.to[symbol];
}
}
}
void CalculateCompressedLink(size_t node) {
auto link_node = link_[node];
if (trie_[link_node].terminal) {
compressed_link_[node] = link_node;
return;
}
compressed_link_[node] = compressed_link_[link_node];
}
void CalculateAnswerForTerminals(size_t node, size_t pos) {
while (node != 0) {
if (trie_[node].terminal) {
for (const auto& dict_pos_node : trie_[node].dict_pos) {
++dict_pos_[dict_pos_node][0];
dict_pos_[dict_pos_node].push_back(pos + 1 - trie_[node].len);
}
}
node = compressed_link_[node];
}
}
AlphaBet alpha_bet_;
std::vector<Node> trie_;
std::vector<ssize_t> link_;
std::vector<ssize_t> compressed_link_;
std::vector<std::vector<ssize_t>> go_;
std::vector<std::vector<size_t>> dict_pos_;
size_t dict_size_ = 0;
};
class Solver {
public:
void Read() {
std::string text;
std::cin >> text;
text_ = std::vector<char>(text.begin(), text.end());
size_t dict_size;
std::cin >> dict_size;
std::string pattern;
for (size_t i = 0; i < dict_size; ++i) {
std::cin >> pattern;
std::vector<char> word(pattern.begin(), pattern.end());
automation_.Add(word);
}
}
void Solve() {
automation_.Init();
answer_ = automation_.Calculate(text_);
}
void Print() {
for (auto& row : answer_) {
std::cout << row[0] << ' ';
for (size_t pos = 1; pos < row.size(); ++pos) {
std::cout << row[pos] + 1 << ' ';
}
std::cout << std::endl;
}
}
private:
static AlphaBet<std::deque<char>> GenerateAlphabet() {
std::deque<char> container('z' - 'a' + 1);
for (size_t pos = 0; pos < container.size(); ++pos) {
container[pos] = 'a' + pos;
}
return AlphaBet<std::deque<char>>(container);
}
AlphaBet<std::deque<char>> alpha_bet_ = GenerateAlphabet();
AhoCorasickAutomation<AlphaBet<std::deque<char>>> automation_ =
AhoCorasickAutomation<AlphaBet<std::deque<char>>>(alpha_bet_);
std::vector<char> text_;
std::vector<std::vector<size_t>> answer_;
};
int main() {
Solver solver;
solver.Read();
solver.Solve();
solver.Print();
}