結果

問題 No.430 文字列検索
ユーザー Kiona1018Kiona1018
提出日時 2019-10-10 23:32:58
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,293 bytes
コンパイル時間 1,417 ms
コンパイル使用メモリ 166,280 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-08-14 08:22:56
合計ジャッジ時間 4,670 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,n,m) for(int i = (n); i <(m); i++)
#define rrep(i,n,m) for(int i = (n) - 1; i >=(m); i--)
using namespace std;
using ll = long long;

int kmp_search(string ref, string key)
{
    vector<int> index(key.size(), 0);
    int i, j;
    i = 0, j = 1;
    while (j < key.size())
    {
        if (key[i] == key[j])
        {
            ++i;
            index[j] = i;
            ++j;
        }
        else if (i == 0)
        {
            index[j] = 0;
            ++j;
        }
        else
            i = index[i-1];
        // cout << "a : " << i << ' ' << j << ' ' << key << endl;
    }

    i = j = 0;
    int cnt = 0;
    while (i < ref.size())
    {
        if (ref[i] == key[j])
            ++i, ++j;
        else if (j == 0)
            ++i;
        else
            j = index[j-1];
        
        if (j == key.size())
            ++cnt, j = index[j-1];
        // cout << "b : " << i << ' ' << j << ' ' << key << endl;
    }
    // cout << cnt << endl;
    return cnt;
}

int main()
{
    string s;
    int m;
    cin >> s >> m;
    int cnt = 0;
    rep(i, 0, m)
    {
        string key;
        scanf("%s", &key);

        cnt += kmp_search(s, key);
        // cout << kmp_search(s, key) << endl;
    }
    cout << cnt << endl;
    return 0;
}
0