結果
| 問題 | No.430 文字列検索 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2019-12-27 10:47:06 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                RE
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,625 bytes | 
| コンパイル時間 | 702 ms | 
| コンパイル使用メモリ | 71,424 KB | 
| 実行使用メモリ | 10,880 KB | 
| 最終ジャッジ日時 | 2024-11-10 00:39:44 | 
| 合計ジャッジ時間 | 3,564 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | RE * 4 | 
| other | RE * 14 | 
コンパイルメッセージ
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));
ソースコード
#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;
}
            
            
            
        