From 9184137474f12c13d605e8d79563a23b0e9763de Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 31 Dec 2022 17:15:29 +0800 Subject: [PATCH] =?UTF-8?q?P3808=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91AC?= =?UTF-8?q?=20=E8=87=AA=E5=8A=A8=E6=9C=BA=EF=BC=88=E7=AE=80=E5=8D=95?= =?UTF-8?q?=E7=89=88=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/98366475 --- Luogu/P3808/P3808.cpp | 60 ++++++++++++------------------------------- 1 file changed, 17 insertions(+), 43 deletions(-) diff --git a/Luogu/P3808/P3808.cpp b/Luogu/P3808/P3808.cpp index 31be4adf..804c835a 100644 --- a/Luogu/P3808/P3808.cpp +++ b/Luogu/P3808/P3808.cpp @@ -1,7 +1,7 @@ #include +#include #include #include -#include using std::cin; using std::cout; @@ -11,38 +11,26 @@ class AcAutomaton { private: struct node { int cnt; - node *child[26], *fail; + std::shared_ptr child[26], fail; node() : cnt(0), fail(nullptr) { - std::fill_n(child, 26, nullptr); - } - - ~node() { - for (int i = 0; i < 26; i++) { - if (child[i] != nullptr) { - delete child[i]; - } - } + std::fill(std::begin(child), std::end(child), nullptr); } }; - node* root; + std::shared_ptr root; public: AcAutomaton() : root(new node()) {} - ~AcAutomaton() { - delete root; - } - void insert(std::string s) { - node* cur = root; + std::shared_ptr cur = root; for (char c : s) { if (cur->child[c - 'a'] == nullptr) { - cur->child[c - 'a'] = new node(); + cur->child[c - 'a'] = std::make_shared(); } cur = cur->child[c - 'a']; @@ -52,11 +40,11 @@ class AcAutomaton { } void build() { - std::queue q; + std::queue> q; for (int i = 0; i < 26; i++) { if (root->child[i] != nullptr) { - q.push(root->child[i]); + q.emplace(root->child[i]); root->child[i]->fail = root; } } @@ -66,41 +54,27 @@ class AcAutomaton { q.pop(); 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; - - while (p != nullptr) { - if (p->child[i] != nullptr) { - cur->child[i]->fail = p->child[i]; - - break; - } - - p = p->fail; + q.emplace(cur->child[i]); + } else { + cur->child[i] = cur->fail->child[i] == nullptr ? root : cur->fail->child[i]; } - - if (p == nullptr) cur->child[i]->fail = root; - - q.push(cur->child[i]); } } } int query(std::string t) { int res = 0; - node* cur = root; + std::shared_ptr cur = root; 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']; - node* p = cur; - - while (p != root && p->cnt >= 0) { - res += p->cnt; - p->cnt = -1; - p = p->fail; + for (std::shared_ptr i = cur; i != nullptr && i->cnt != -1; i = i->fail) { + res += i->cnt; + i->cnt = -1; } }