-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaho_corasick.cpp
34 lines (30 loc) · 1.06 KB
/
aho_corasick.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
#include "test_utils.hpp"
#include "strings/aho_corasick.hpp"
struct visitor {
int cnt = 0;
const vector<string>& words;
explicit visitor(const vector<string>& words) : words(words) {}
void operator()(int i, int wordid) {
print("{:3}: index {:2} - {}\n", cnt++, i, words[wordid]);
}
};
void unit_test_aho_corasick() {
vector<string> words = {
"aaab", "aab", "aaba", "aabb", "abc", "aca", "acb", "acba",
"acbaa", "ba", "bba", "bbbbc", "bbbc", "bbbda", "bbc", "bc",
"caa", "cadba", "cadbc", "cb", "cc", "ccaa", "ccb",
};
string text = "aabababacbcabbaccadbcbacbabcabcababa";
aho_corasick aho(words);
print("states: {}\n", aho.num_nodes());
print("matches: {}\n", aho.count_matches(text));
aho.visit_all(text, visitor(words));
print("unique matches: {}\n", aho.count_unique_matches(text));
aho.visit_longest_each_index(text, visitor(words));
auto v = aho.longest_each_index(text);
cout << v << endl;
}
int main() {
RUN_SHORT(unit_test_aho_corasick());
return 0;
}