結果

問題 No.430 文字列検索
ユーザー boutarouboutarou
提出日時 2021-01-04 16:26:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 1,159 bytes
コンパイル時間 822 ms
コンパイル使用メモリ 89,848 KB
実行使用メモリ 27,392 KB
最終ジャッジ日時 2024-04-23 02:40:15
合計ジャッジ時間 2,278 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 253 ms
27,392 KB
testcase_02 AC 14 ms
6,940 KB
testcase_03 AC 15 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 227 ms
26,880 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 15 ms
6,940 KB
testcase_11 AC 60 ms
8,192 KB
testcase_12 AC 61 ms
8,320 KB
testcase_13 AC 63 ms
8,192 KB
testcase_14 AC 51 ms
6,944 KB
testcase_15 AC 38 ms
6,944 KB
testcase_16 AC 17 ms
6,940 KB
testcase_17 AC 15 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
using namespace std;
using ll = long long;

struct RollingHash {
    ll base = 1007, MOD = 1e9 + 7;
    vector<ll> hash, pow;

    RollingHash(string s) {
        int n = (int)s.size();
        hash.assign(n + 1, 0);
        pow.assign(n + 1, 1);
        for (int i = 0; i < n; i++) {
            hash[i + 1] = (hash[i] * base + s[i]) % MOD;
            pow[i + 1] = pow[i] * base % MOD;
        }
    }

    ll get(int l, int r) {
        ll res = (hash[r] - hash[l] * pow[r - l]) % MOD;
        if (res < 0)
            res += MOD;
        return res;
    }
};

int main() {
    string s; cin >> s;
    int m; cin >> m;
    vector<string> c(m);
    for (int i = 0; i < m; i++) cin >> c[i];
    RollingHash rh(s);
    map<ll, ll> mp;
    for (int i = 1; i <= 10; i++) {
        for (int j = 0; j + i <= (int)s.size(); j++) {
            ll hash = rh.get(j, j + i);
            mp[hash]++;
        }
    }
    ll ans = 0;
    for (int i = 0; i < m; i++) {
        RollingHash now(c[i]);
        ll hash = now.get(0, (int)c[i].size());
        ans += mp[hash];
    }
    cout << ans << endl;
    return 0;
}
0