結果

問題 No.430 文字列検索
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2022-09-21 01:58:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,449 bytes
コンパイル時間 1,795 ms
コンパイル使用メモリ 175,816 KB
実行使用メモリ 12,856 KB
最終ジャッジ日時 2023-08-23 19:46:47
合計ジャッジ時間 6,247 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,756 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2022.09.21 01:58:39
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;


struct Trie {
    struct Node {
        array<int,26> ch;
        int cnt;            
        int sub;            
        Node () : cnt(0), sub(0) {
            fill(ch.begin(), ch.end(), -1);
        }
    };

    vector<Node> nodes;

    Trie() : nodes(1) {}

    void insert(const string &s) {
        int t = 0;
        for (int i = 0; i < (int)s.size(); i++) {
            if (nodes[t].ch[s[i]-'A'] == -1) {
                nodes[t].ch[s[i]-'A'] = nodes.size();
                nodes.push_back(Node());
            }
            nodes[t].sub++;
            t = nodes[t].ch[s[i]-'A'];
        }
        nodes[t].sub++;
        nodes[t].cnt++;
    }
};

struct AhoCorasick {
    Trie trie;
    vector<int> failure;
    vector<int> count;
    AhoCorasick() {}
    void insert(const string &s) {
        trie.insert(s);
    }
    void build() {
        for (int c = 0; c < 26; c++) {
            if (trie.nodes[0].ch[c] != -1) continue;
            trie.nodes[0].ch[c] = 0;
        }
        failure.resize(trie.nodes.size(),-1);
        count.resize(trie.nodes.size(),0);
        queue<int> que;
        que.push(0);
        while(!que.empty()) {
            int v = que.front(); que.pop();
            for (int c = 0; c < 26; c++) {
                int to = trie.nodes[v].ch[c];
                if (to <= 0) continue;
                count[to] = trie.nodes[to].cnt;
                que.push(to);
                int now = v;
                while(now != 0 && trie.nodes[failure[now]].ch[c] == -1) {
                    now = failure[v];
                }
                failure[to] = (now==0?0:trie.nodes[failure[now]].ch[c]);
                count[to] += count[failure[to]];
            }
        }
    }

    int move(int idx, int c) {
        if (trie.nodes[idx].ch[c] == -1) {
            return move(failure[idx],c);
        }
        else {
            return trie.nodes[idx].ch[c];
        }
    }
};

int main() {
    string s;cin >> s;
    int n;cin >> n;
    AhoCorasick aho;
    for (int i = 0; i < n; i++) {
        string t;cin >> t;
        aho.insert(t);
    }
    aho.build();
    int now = 0;
    int ans = 0;
    for (int i = 0; i < s.size(); i++) {
        now = aho.move(now,s[i]-'A');
        ans += aho.count[now];
    }
    cout << ans << endl;

    return 0;
}
0