結果

問題 No.430 文字列検索
ユーザー face4face4
提出日時 2019-12-27 10:47:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,646 bytes
コンパイル時間 675 ms
コンパイル使用メモリ 71,676 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-16 17:26:41
合計ジャッジ時間 1,442 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 15 ms
11,008 KB
testcase_02 AC 7 ms
5,376 KB
testcase_03 AC 7 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 12 ms
7,136 KB
testcase_12 AC 13 ms
7,164 KB
testcase_13 AC 13 ms
7,288 KB
testcase_14 AC 10 ms
7,204 KB
testcase_15 AC 9 ms
7,132 KB
testcase_16 AC 9 ms
7,132 KB
testcase_17 AC 9 ms
7,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;

struct Node{
    int nex[26];
    bool leaf = false;
    int p = -1;
    char pch;
    int link = -1;
    int go[26];
    int acc = 0;
    bool done = false;
    Node(int p=-1, char ch='$') : p(p), pch(ch){
        fill(begin(nex), end(nex), -1);
        fill(begin(go), end(go), -1);
    }
};

vector<Node> t(1);

void add_string(const string &s){
    int v = 0;
    for(char c : s){
        int ind = c-'A';
        if(t[v].nex[ind] == -1){
            t[v].nex[ind] = t.size();
            t.emplace_back(v, c);
        }
        v = t[v].nex[ind];
    }
    t[v].leaf = true;
}

int go(int v, char ch);

int get_link(int v){
    if(t[v].link == -1){
        // rootのsuffix linkはroot,1文字のsuffix linkもroot
        if(v == 0 || t[v].p == 0){
            t[v].link = 0;
        }else{
            t[v].link = go(get_link(t[v].p), t[v].pch);
        }
    }
    return t[v].link;
}

int go(int v, char ch){
    int c = ch-'A';
    if(t[v].go[c] == -1){
        if(t[v].nex[c] != -1){
            t[v].go[c] = t[v].nex[c];
        }else{
            t[v].go[c] = v==0 ? 0 : go(get_link(v), ch);
        }
    }
    return t[v].go[c];
}

int calc(int v){
    if(t[v].done){
        return t[v].acc;
    }
    t[v].done = true;
    t[v].acc = t[v].leaf + calc(get_link(v));
    return t[v].acc;
}

int main(){
    string s;
    int m;
    cin >> s >> m;
    while(m--){
        string x;
        cin >> x;
        add_string(x);
    }
    int now = 0, ans = 0;
    for(char c : s){
        now = go(now, c);
        ans += calc(now);
    }
    cout << ans << endl;
    return 0;
}
0