結果

問題 No.430 文字列検索
ユーザー Kiona1018Kiona1018
提出日時 2019-10-10 23:31:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,286 bytes
コンパイル時間 1,506 ms
コンパイル使用メモリ 168,484 KB
実行使用メモリ 6,940 KB
最終ジャッジ日時 2024-05-01 20:52:21
合計ジャッジ時間 17,590 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 497 ms
5,376 KB
testcase_02 AC 1,437 ms
5,376 KB
testcase_03 AC 818 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 1,957 ms
5,376 KB
testcase_12 AC 1,925 ms
5,376 KB
testcase_13 AC 1,948 ms
5,376 KB
testcase_14 TLE -
testcase_15 AC 1,933 ms
5,376 KB
testcase_16 AC 1,503 ms
5,376 KB
testcase_17 AC 1,439 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

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;
        cin >> key;

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