結果
| 問題 | No.430 文字列検索 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-11-13 16:40:53 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,745 ms / 2,000 ms | 
| コード長 | 1,336 bytes | 
| コンパイル時間 | 2,004 ms | 
| コンパイル使用メモリ | 194,216 KB | 
| 最終ジャッジ日時 | 2025-01-15 22:19:56 | 
| ジャッジサーバーID (参考情報) | judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 14 | 
ソースコード
#ifdef LOCAL
#define debug(_) cerr << #_ << ": " << (_) << '\n'
#define _GLIBCXX_DEBUG
#else
#define debug(_) (void(0))
#endif  // LOCAL
#include <bits/stdc++.h>
using namespace std;
int kmp_count(string &s, string &w) {
    // テーブル構築
    vector<int> T(w.size() + 1, 0);
    T[0] = -1;
    int i = 2, j = 0;
    while (i <= w.size()) {
        if (w[j] == w[i - 1]) {
            j++;
            T[i] = j;
            i++;
        } else if (j > 0) {
            j = T[j];
        } else {
            i++;
        }
    }
    // 検索
    int ret = 0;
    int m = 0;
    i = 0;
    while (m + i < s.size()) {
        if (w[i] == s[m + i]) {
            i++;
            if (i == w.size()) {
                ret++;
                debug(m);
                for (int t = 0; t < w.size(); t++) debug(s[m + t]);
                m = m + i - T[i];
                i = T[i];
            }
        } else {
            m = m + i - T[i];
            if (i > 0) {
                i = T[i];
            }
        }
    }
    debug(ret);
    // cout << ret << endl;
    return ret;
}
int main(){
    string S;
    cin >> S;
    int M;
    cin >> M;
    string C;
    int ans = 0;
    for (int i = 0; i < M; i++){
        cin >> C;
        debug(C);
        ans += kmp_count(S, C);
    }
    cout << ans << '\n';
    return 0;
}
            
            
            
        