結果
| 問題 | No.430 文字列検索 | 
| コンテスト | |
| ユーザー |  vjudge1 | 
| 提出日時 | 2025-10-19 11:29:33 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                WA
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,070 bytes | 
| コンパイル時間 | 635 ms | 
| コンパイル使用メモリ | 69,692 KB | 
| 実行使用メモリ | 7,720 KB | 
| 最終ジャッジ日時 | 2025-10-19 11:29:46 | 
| 合計ジャッジ時間 | 13,165 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 WA * 3 | 
| other | AC * 10 WA * 4 | 
ソースコード
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<int> bnxt(const string &pat) {
    int m = pat.size();
    vector<int> nxt(m, 0);
    int j = 0;
    for (int i = 1; i < m; i++) {
        while (j && pat[i] != pat[j])
            j = nxt[j - 1];
        if (pat[i] == pat[j])
            j++;
        nxt[i] = j;
    }
    return nxt;
}
int kmp(const string &txt, const string &pat) {
    int n = txt.size(), m = pat.size();
    if (m == 0) return 0;
    vector<int> nxt = bnxt(pat);
    int cnt = 0, j = 0;
    for (int i = 0; i < n; i++) {
        while (j && txt[i] != pat[j])
            j = nxt[j - 1];
        if (txt[i] == pat[j])
            j++;
        if (j == m) {
            cnt++;
            j = nxt[j - 1];
        }
    }
    return cnt;
}
int main() {
    ios::sync_with_stdio(false);
    string rope;
    getline(cin, rope);
    int t;
    cin >> t;
    int ans = 0;
    while (t--) {
        string knot;
        getline(cin, knot);
        ans += kmp(rope, knot);
    }
    cout << ans << "\n";
    return 0;
}
            
            
            
        