結果

問題 No.430 文字列検索
ユーザー Kiona1018Kiona1018
提出日時 2019-10-10 23:05:11
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,266 bytes
コンパイル時間 1,716 ms
コンパイル使用メモリ 165,168 KB
実行使用メモリ 7,964 KB
最終ジャッジ日時 2023-08-14 07:33:38
合計ジャッジ時間 5,202 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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[j-1]-1;
        // cout << i << ' ' << j << endl;
    }
    // rep(i, 0, index.size())
    //     cout << index[i] << 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 << cnt << endl;
    return cnt;
}

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

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