mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-27 15:36:27 +00:00
parent
4cd9aa2cfa
commit
9184137474
@ -1,7 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
using std::cin;
|
using std::cin;
|
||||||
using std::cout;
|
using std::cout;
|
||||||
@ -11,38 +11,26 @@ class AcAutomaton {
|
|||||||
private:
|
private:
|
||||||
struct node {
|
struct node {
|
||||||
int cnt;
|
int cnt;
|
||||||
node *child[26], *fail;
|
std::shared_ptr<node> child[26], fail;
|
||||||
|
|
||||||
node()
|
node()
|
||||||
: cnt(0), fail(nullptr) {
|
: cnt(0), fail(nullptr) {
|
||||||
std::fill_n(child, 26, nullptr);
|
std::fill(std::begin(child), std::end(child), nullptr);
|
||||||
}
|
|
||||||
|
|
||||||
~node() {
|
|
||||||
for (int i = 0; i < 26; i++) {
|
|
||||||
if (child[i] != nullptr) {
|
|
||||||
delete child[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
node* root;
|
std::shared_ptr<node> root;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
AcAutomaton()
|
AcAutomaton()
|
||||||
: root(new node()) {}
|
: root(new node()) {}
|
||||||
|
|
||||||
~AcAutomaton() {
|
|
||||||
delete root;
|
|
||||||
}
|
|
||||||
|
|
||||||
void insert(std::string s) {
|
void insert(std::string s) {
|
||||||
node* cur = root;
|
std::shared_ptr<node> cur = root;
|
||||||
|
|
||||||
for (char c : s) {
|
for (char c : s) {
|
||||||
if (cur->child[c - 'a'] == nullptr) {
|
if (cur->child[c - 'a'] == nullptr) {
|
||||||
cur->child[c - 'a'] = new node();
|
cur->child[c - 'a'] = std::make_shared<node>();
|
||||||
}
|
}
|
||||||
|
|
||||||
cur = cur->child[c - 'a'];
|
cur = cur->child[c - 'a'];
|
||||||
@ -52,11 +40,11 @@ class AcAutomaton {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void build() {
|
void build() {
|
||||||
std::queue<node*> q;
|
std::queue<std::shared_ptr<node>> q;
|
||||||
|
|
||||||
for (int i = 0; i < 26; i++) {
|
for (int i = 0; i < 26; i++) {
|
||||||
if (root->child[i] != nullptr) {
|
if (root->child[i] != nullptr) {
|
||||||
q.push(root->child[i]);
|
q.emplace(root->child[i]);
|
||||||
root->child[i]->fail = root;
|
root->child[i]->fail = root;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -66,41 +54,27 @@ class AcAutomaton {
|
|||||||
q.pop();
|
q.pop();
|
||||||
|
|
||||||
for (int i = 0; i < 26; i++) {
|
for (int i = 0; i < 26; i++) {
|
||||||
if (cur->child[i] == nullptr) continue;
|
if (cur->child[i] != nullptr) {
|
||||||
|
cur->child[i]->fail = cur->fail->child[i] == nullptr ? root : cur->fail->child[i];
|
||||||
|
|
||||||
auto p = cur->fail;
|
q.emplace(cur->child[i]);
|
||||||
|
} else {
|
||||||
while (p != nullptr) {
|
cur->child[i] = cur->fail->child[i] == nullptr ? root : cur->fail->child[i];
|
||||||
if (p->child[i] != nullptr) {
|
|
||||||
cur->child[i]->fail = p->child[i];
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
p = p->fail;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p == nullptr) cur->child[i]->fail = root;
|
|
||||||
|
|
||||||
q.push(cur->child[i]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int query(std::string t) {
|
int query(std::string t) {
|
||||||
int res = 0;
|
int res = 0;
|
||||||
node* cur = root;
|
std::shared_ptr<node> cur = root;
|
||||||
|
|
||||||
for (char c : t) {
|
for (char c : t) {
|
||||||
while (cur != root && cur->child[c - 'a'] == nullptr) cur = cur->fail;
|
|
||||||
cur = cur->child[c - 'a'] == nullptr ? root : cur->child[c - 'a'];
|
cur = cur->child[c - 'a'] == nullptr ? root : cur->child[c - 'a'];
|
||||||
|
|
||||||
node* p = cur;
|
for (std::shared_ptr<node> i = cur; i != nullptr && i->cnt != -1; i = i->fail) {
|
||||||
|
res += i->cnt;
|
||||||
while (p != root && p->cnt >= 0) {
|
i->cnt = -1;
|
||||||
res += p->cnt;
|
|
||||||
p->cnt = -1;
|
|
||||||
p = p->fail;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user