結果

問題 No.430 文字列検索
ユーザー face4face4
提出日時 2019-12-27 10:47:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,625 bytes
コンパイル時間 849 ms
コンパイル使用メモリ 71,792 KB
実行使用メモリ 11,132 KB
最終ジャッジ日時 2024-04-16 17:25:09
合計ジャッジ時間 4,910 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int calc(int)':
main.cpp:66:14: warning: control reaches end of non-void function [-Wreturn-type]
   66 |     t[v].acc = t[v].leaf + calc(get_link(v));

ソースコード

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));
}

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