結果

問題 No.430 文字列検索
ユーザー zkouzkou
提出日時 2020-11-13 15:46:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,163 bytes
コンパイル時間 2,314 ms
コンパイル使用メモリ 199,160 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-30 02:07:25
合計ジャッジ時間 17,455 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
    for (i = 2; i <= w.size(); i++){
        if (w[j] == w[i - 1]) {
            j++;
            T[i] = j;
        } else if (j > 0) {
            j = T[j];
        }
    }
    // 検索
    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++;
                m = m + i - T[i];
                i = T[i];
            }
        } else {
            m = m + i - T[i];
            if (i > 0) {
                i = T[i];
            }
        }
    }
    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;
        ans += kmp_count(S, C);
    }
    cout << ans << '\n';

    return 0;
}
0