結果
| 問題 | No.430 文字列検索 | 
| コンテスト | |
| ユーザー |  sotanishy | 
| 提出日時 | 2021-10-07 17:57:05 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 28 ms / 2,000 ms | 
| コード長 | 2,758 bytes | 
| コンパイル時間 | 4,829 ms | 
| コンパイル使用メモリ | 215,200 KB | 
| 最終ジャッジ日時 | 2025-01-24 21:31:27 | 
| ジャッジサーバーID (参考情報) | judge5 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 14 | 
ソースコード
#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;
}
            
            
            
        