src/watashi/ac-automata.hpp
Code
#include <algorithm>
#include <queue>
using namespace std;
struct AhoCorasick {
static const int NONE = 0;
static const int MAXN = 1024;
static const int CHARSET = 26;
int end;
int tag[MAXN];
int fail[MAXN];
int trie[MAXN][CHARSET];
void init() {
tag[0] = NONE;
fill(trie[0], trie[0] + CHARSET, -1);
end = 1;
}
int add(int m, const int *s) {
int p = 0;
for (int i = 0; i < m; ++i) {
if (trie[p][*s] == -1) {
tag[end] = NONE;
fill(trie[end], trie[end] + CHARSET, -1);
trie[p][*s] = end++;
}
p = trie[p][*s];
++s;
}
return p;
}
void build(void) { // !!
queue<int> bfs;
fail[0] = 0;
for (int i = 0; i < CHARSET; ++i) {
if (trie[0][i] != -1) {
fail[trie[0][i]] = 0;
bfs.push(trie[0][i]);
} else {
trie[0][i] = 0;
}
}
while (!bfs.empty()) {
int p = bfs.front();
tag[p] |= tag[fail[p]];
bfs.pop();
for (int i = 0; i < CHARSET; ++i) {
if (trie[p][i] != -1) {
fail[trie[p][i]] = trie[fail[p]][i];
bfs.push(trie[p][i]);
} else {
trie[p][i] = trie[fail[p]][i];
}
}
}
}
} ac;
#line 1 "src/watashi/ac-automata.hpp"
#include <algorithm>
#include <queue>
using namespace std;
struct AhoCorasick {
static const int NONE = 0;
static const int MAXN = 1024;
static const int CHARSET = 26;
int end;
int tag[MAXN];
int fail[MAXN];
int trie[MAXN][CHARSET];
void init() {
tag[0] = NONE;
fill(trie[0], trie[0] + CHARSET, -1);
end = 1;
}
int add(int m, const int *s) {
int p = 0;
for (int i = 0; i < m; ++i) {
if (trie[p][*s] == -1) {
tag[end] = NONE;
fill(trie[end], trie[end] + CHARSET, -1);
trie[p][*s] = end++;
}
p = trie[p][*s];
++s;
}
return p;
}
void build(void) { // !!
queue<int> bfs;
fail[0] = 0;
for (int i = 0; i < CHARSET; ++i) {
if (trie[0][i] != -1) {
fail[trie[0][i]] = 0;
bfs.push(trie[0][i]);
} else {
trie[0][i] = 0;
}
}
while (!bfs.empty()) {
int p = bfs.front();
tag[p] |= tag[fail[p]];
bfs.pop();
for (int i = 0; i < CHARSET; ++i) {
if (trie[p][i] != -1) {
fail[trie[p][i]] = trie[fail[p]][i];
bfs.push(trie[p][i]);
} else {
trie[p][i] = trie[fail[p]][i];
}
}
}
}
} ac;
Back to top page