結果

問題 No.430 文字列検索
ユーザー rogi52rogi52
提出日時 2022-02-21 16:52:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,584 ms / 2,000 ms
コード長 1,651 bytes
コンパイル時間 1,587 ms
コンパイル使用メモリ 169,948 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-12 08:23:42
合計ジャッジ時間 18,187 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1,574 ms
4,376 KB
testcase_02 AC 1,569 ms
4,380 KB
testcase_03 AC 1,570 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 8 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 1,571 ms
4,376 KB
testcase_12 AC 1,584 ms
4,380 KB
testcase_13 AC 1,571 ms
4,376 KB
testcase_14 AC 1,570 ms
4,376 KB
testcase_15 AC 1,566 ms
4,380 KB
testcase_16 AC 1,568 ms
4,376 KB
testcase_17 AC 1,569 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct RollingHash {
    static constexpr int M = 1;
    static constexpr long long MODS[M] = {999999937};
    static constexpr long long BASE = 9973;
    vector<long long> powb[M], hash[M];
    int n;
    RollingHash() {}
    RollingHash(const string& str) { init(str); }
    void init(const string& str) {
        n = str.size();
        for (int k = 0; k < M; k++) {
            powb[k].resize(n + 1, 1);
            hash[k].resize(n + 1, 0);
            for (int i = 0; i < n; i++) {
                hash[k][i + 1] = (hash[k][i] * BASE + str[i]) % MODS[k];
                powb[k][i + 1] = powb[k][i] * BASE % MODS[k];
            }
        }
    }
    // get hash str[l,r)
    long long get(int l, int r, int k) {
        long long res = hash[k][r] - hash[k][l] * powb[k][r - l] % MODS[k];
        if (res < 0) res += MODS[k];
        return res;
    }
};

bool match(RollingHash& rh1, int l1, int r1, RollingHash& rh2, int l2, int r2) {
    bool res = true;
    for (int k = 0; k < RollingHash::M; k++) {
        res &= rh1.get(l1, r1, k) == rh2.get(l2, r2, k);
    }
    return res;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    string S; cin >> S;
    RollingHash HS(S);
    int N = S.size();

    int M; cin >> M;
    int ans = 0;
    rep(_,M) {
        string C; cin >> C;
        RollingHash HC(C);
        int len = C.size();
        for(int i = 0; i + len <= N; i++) {
            if(match(HS, i, i + len, HC, 0, len)) {
                ans++;
            }
        }
    }
    cout << ans << endl;
}
0