結果

問題 No.430 文字列検索
ユーザー legosukelegosuke
提出日時 2021-11-12 22:00:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 395 ms / 2,000 ms
コード長 1,768 bytes
コンパイル時間 2,283 ms
コンパイル使用メモリ 207,912 KB
実行使用メモリ 26,472 KB
最終ジャッジ日時 2023-08-17 01:24:58
合計ジャッジ時間 4,442 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 395 ms
26,472 KB
testcase_02 AC 12 ms
4,380 KB
testcase_03 AC 12 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 375 ms
26,196 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 16 ms
5,716 KB
testcase_11 AC 67 ms
7,388 KB
testcase_12 AC 67 ms
7,384 KB
testcase_13 AC 68 ms
7,480 KB
testcase_14 AC 51 ms
5,860 KB
testcase_15 AC 40 ms
5,192 KB
testcase_16 AC 14 ms
4,376 KB
testcase_17 AC 13 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int64_t random_number(int64_t n) {
    mt19937_64 mt(std::chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<int64_t> dist(0, n - 1);
    return dist(mt);
}

const int64_t MOD = ((int64_t)1 << 61) - 1;
const int64_t MASK30 = ((int64_t)1 << 30) - 1;
const int64_t MASK31 = ((int64_t)1 << 31) - 1;
const int64_t MASK61 = ((int64_t)1 << 61) - 1;
int64_t base = random_number(MOD);

int64_t mod(int64_t x) {
    int64_t res = (x >> 61) + (x & MASK61);
    if (res >= MOD) res -= MOD;
    return res;
}

int64_t mul(int64_t a, int64_t b) {
    int64_t au = (a >> 31), ad = (a & MASK31);
    int64_t bu = (b >> 31), bd = (b & MASK31);
    int64_t c = (au * bd + ad * bu);
    int64_t cu = (c >> 30), cd = (c & MASK30);
    return mod(au * bu * 2 + cu + (cd << 31) + ad * bd);
}

int64_t add(int64_t a, int64_t b) {
    return mod(a + b);
}

int64_t sub(int64_t a, int64_t b) {
    int64_t res = a - b;
    if (res < 0) res += MOD;
    return res;
}

int main() {
    string S;
    int M;
    cin >> S >> M;
    map<int64_t, int> mp;
    for (int k = 1; k <= 10; ++k) {
        int64_t h = 0, t = 1;
        for (int i = 0; i < k; ++i) {
            h = add(mul(h, base), S[i]);
            t = mul(t, base);
        }
        for (int i = 0; i + k <= (int)S.size(); ++i) {
            mp[h]++;
            if (i + k < (int)S.size()) {
                h = sub(add(mul(h, base), S[i + k]), mul(S[i], t));
            }
        }
    }
    int ans = 0;
    for (int q = 0; q < M; ++q) {
        string C;
        cin >> C;
        int64_t ch = 0;
        for (auto&& c : C) {
            ch = add(mul(ch, base), c);
        }
        ans += mp[ch];
    }
    cout << ans << endl;
    return 0;
}
0