結果

問題 No.430 文字列検索
ユーザー veqccveqcc
提出日時 2019-07-16 22:21:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,384 bytes
コンパイル時間 930 ms
コンパイル使用メモリ 105,256 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-21 11:15:36
合計ジャッジ時間 15,412 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;

class KMP {
    string pattern;
    vector <int> fail;

    void init(const string &p) {
        pattern = p;
        int m = pattern.size();
        fail.assign(m + 1, -1);
        for (int i = 0, j = -1; i < m; i++) {
            while (j >= 0 && pattern[i] != pattern[j]) j = fail[j];
            fail[i + 1] = ++j;
        }
    }

public:
    KMP(const string &p) { init(p); }

    int period(int i) { return i - fail[i]; }

    vector <int> match(const string &s) {
        int n = s.size();
        int m = pattern.size();
        vector <int> res;
        for (int i = 0, k = 0; i < n; i++) {
            while (k >= 0 && s[i] != pattern[k]) k = fail[k];
            k++;
            if (k == m) res.push_back(i - m + 1);
        }
        return res;
    }
};

int main() {
    string s;
    cin >> s;

    int m;
    cin >> m;

    int ans = 0;
    for (int i = 0; i < m; i++) {
        string pattern;
        cin >> pattern;

        KMP kmp(pattern);
        vector <int> res = kmp.match(s);
        ans += res.size();
    }

    cout << ans << "\n";
    return 0;
}
0