結果

問題 No.430 文字列検索
ユーザー 629e^-b629e^-b
提出日時 2021-04-28 12:37:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 3,969 bytes
コンパイル時間 4,157 ms
コンパイル使用メモリ 265,608 KB
実行使用メモリ 9,360 KB
最終ジャッジ日時 2023-09-21 12:54:37
合計ジャッジ時間 5,364 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 12 ms
9,360 KB
testcase_02 AC 4 ms
5,028 KB
testcase_03 AC 4 ms
4,956 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 8 ms
7,132 KB
testcase_12 AC 8 ms
7,764 KB
testcase_13 AC 9 ms
7,744 KB
testcase_14 AC 6 ms
6,688 KB
testcase_15 AC 6 ms
5,728 KB
testcase_16 AC 5 ms
5,724 KB
testcase_17 AC 5 ms
5,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
template <typename T>
using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;
const ld PI = acos(-1);

template <int char_size, int base>
struct Trie {
    struct Node {
        Node *next[char_size];
        int exist;
        vector<int> accept;

        Node() : exist(0) {
            memset(next, 0, sizeof(next));
        }

        ~Node() {
            for (int i = 0; i < char_size; ++i) {
                if (next[i] != nullptr) {
                    delete next[i];
                }
            }
        }
    };

    Node *root;
    int size;

    Trie() : root(new Node), size(1) {}

    void add(const string &str, int id) {
        Node *now = root;

        for (int i = 0; i < str.size(); ++i) {
            int c = str[i] - base;

            if (now->next[c] == nullptr) {
                now->next[c] = new Node;
                size++;
            }
            (now->exist)++;
            now = now->next[c];
        }

        (now->exist)++;
        now->accept.emplace_back(id);
    }

    void add(const string &str) {
        add(str, root->exist);
    }

    bool query(const string &str, bool prefix = false) {
        Node *now = root;

        for (int i = 0; i < str.size(); ++i) {
            int c = str[i] - base;
            if (now->next[c] == nullptr)
                return false;
            now = now->next[c];
        }

        return prefix | (now->accept.size() > 0);
    }

    int count() const {
        return root->exist;
    }
};

template <int char_size, int base>
struct AhoCorasick : Trie<char_size + 1, base> {
    using typename Trie<char_size + 1, base>::Node;

    const int FAIL = char_size;

    void build() {
        queue<Node *> que;
        for (int i = 0; i <= char_size; ++i) {
            Node *&next_node = this->root->next[i];
            if (next_node != nullptr) {
                next_node->next[FAIL] = this->root;
                que.emplace(next_node);
            } else {
                next_node = this->root;
            }
        }

        while (!que.empty()) {
            Node *now = que.front();
            Node *fail_node = now->next[FAIL];
            que.pop();
            for (int i = 0; i < char_size; ++i) {
                if (now->next[i] == nullptr) {
                    now->next[i] = fail_node->next[i];
                    continue;
                }
                while (fail_node->next[i] == nullptr)
                    fail_node = fail_node->next[FAIL];
                now->next[i]->next[FAIL] = fail_node->next[i];

                vector<int> accept;
                vector<int> &u = now->next[i]->accept;
                vector<int> &v = fail_node->next[i]->accept;
                set_union(u.begin(), u.end(), v.begin(), v.end(), back_inserter(accept));
                swap(u, accept);

                que.push(now->next[i]);
            }
        }
    }

    vector<int> match(const string &str) {
        Node *now = this->root;
        vector<int> res(this->count());
        for (int i = 0; i < str.size(); ++i) {
            int c = str[i] - base;
            now = now->next[c];
            for (int j = 0; j < now->accept.size(); ++j) {
                res[now->accept[j]]++;
            }
        }
        return res;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    string S;
    cin >> S;
    int M;
    cin >> M;
    AhoCorasick<26, 'A'> ahc;
    for (int i = 0; i < M; ++i) {
        string C;
        cin >> C;
        ahc.add(C);
    }

    ahc.build();

    auto res = ahc.match(S);
    ll ans = 0;
    for (auto i : res) {
        ans += i;
    }
    cout << ans << endl;

    return 0;
}
0