結果

問題 No.430 文字列検索
ユーザー sotanishysotanishy
提出日時 2021-10-07 17:57:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 2,758 bytes
コンパイル時間 2,569 ms
コンパイル使用メモリ 221,576 KB
実行使用メモリ 15,024 KB
最終ジャッジ日時 2023-09-30 09:04:27
合計ジャッジ時間 3,646 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 32 ms
15,024 KB
testcase_02 AC 7 ms
6,412 KB
testcase_03 AC 6 ms
6,668 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 20 ms
10,920 KB
testcase_12 AC 22 ms
12,184 KB
testcase_13 AC 22 ms
12,128 KB
testcase_14 AC 16 ms
10,012 KB
testcase_15 AC 13 ms
8,032 KB
testcase_16 AC 11 ms
7,988 KB
testcase_17 AC 10 ms
8,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template <int Size, int Offset>
class Trie {
public:
    Trie() : root(std::make_shared<Node>()) {}

    void insert(const std::string& s, int id) { insert(root, s, id, 0); }

protected:
    struct Node;
    using node_ptr = std::shared_ptr<Node>;

    struct Node {
        std::vector<node_ptr> ch;
        std::vector<int> accept;
        int sz = 0;

        Node() : ch(Size) {}
    };

    const node_ptr root;

    void insert(const node_ptr& t, const std::string& s, int id, int k) {
        ++t->sz;
        if (k == (int) s.size()) {
            t->accept.push_back(id);
            return;
        }
        int c = s[k] - Offset;
        if (!t->ch[c]) t->ch[c] = std::make_shared<Node>();
        insert(t->ch[c], s, id, k + 1);
    }
};


template <int Size, int Offset>
class AhoCorasick : public Trie<Size + 1, Offset> {
    using node_ptr = typename Trie<Size + 1, Offset>::node_ptr;
    using Trie<Size + 1, Offset>::root;

    static const int FAIL = Size;

public:
    void build() {
        std::queue<node_ptr> que;
        for (int i = 0; i <= Size; ++i) {
            if (root->ch[i]) {
                root->ch[i]->ch[FAIL] = root;
                que.push(root->ch[i]);
            } else {
                root->ch[i] = root;
            }
        }
        while (!que.empty()) {
            auto t = que.front();
            que.pop();
            for (int i = 0; i < Size; ++i) {
                if (!t->ch[i]) continue;
                auto fail = t->ch[FAIL];
                while (!fail->ch[i]) fail = fail->ch[FAIL];
                t->ch[i]->ch[FAIL] = fail->ch[i];

                auto& u = t->ch[i]->accept;
                auto& v = fail->ch[i]->accept;
                std::vector<int> accept;
                std::set_union(u.begin(), u.end(), v.begin(), v.end(), std::back_inserter(accept));
                u = accept;

                que.push(t->ch[i]);
            }
        }
    }

    std::map<int, int> match(const std::string& str) const {
        std::map<int, int> ret;
        auto t = root;
        for (auto c : str) {
            while (!t->ch[c - Offset]) t = t->ch[FAIL];
            t = t->ch[c - Offset];
            for (auto& i : t->accept) ++ret[i];
        }
        return ret;
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);


    string S;
    cin >> S;
    int M;
    cin >> M;
    AhoCorasick<26, 'A'> aho;
    for (int i = 0; i < M; ++i) {
        string s;
        cin >> s;
        aho.insert(s, i);
    }
    aho.build();
    auto ret = aho.match(S);
    ll ans = 0;
    for (int i = 0; i < M; ++i) ans += ret[i];
    cout << ans << endl;
}
0