結果

問題 No.430 文字列検索
ユーザー みずくらげみずくらげ
提出日時 2019-11-17 17:39:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 986 ms / 2,000 ms
コード長 2,212 bytes
コンパイル時間 1,345 ms
コンパイル使用メモリ 107,740 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-25 21:16:37
合計ジャッジ時間 11,793 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 974 ms
4,372 KB
testcase_02 AC 977 ms
4,372 KB
testcase_03 AC 978 ms
4,372 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 1 ms
4,372 KB
testcase_06 AC 1 ms
4,372 KB
testcase_07 AC 2 ms
4,372 KB
testcase_08 AC 7 ms
4,372 KB
testcase_09 AC 2 ms
4,372 KB
testcase_10 AC 5 ms
4,372 KB
testcase_11 AC 980 ms
4,372 KB
testcase_12 AC 980 ms
4,372 KB
testcase_13 AC 986 ms
4,372 KB
testcase_14 AC 975 ms
4,372 KB
testcase_15 AC 980 ms
4,372 KB
testcase_16 AC 982 ms
4,372 KB
testcase_17 AC 978 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <iomanip>
#include <cassert>
#include <random>
#include <tuple>
#include <cstdint>

#define rep(i,n) for (int i = 0; i < (n); ++i)

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const int INF = INT32_MAX;


random_device seed_gen;
mt19937 engine(seed_gen());
uniform_int_distribution<int> rand_dist(1e4, 1e9);

struct RollingHash {
    const uint64_t MASK30 = (1uLL << 30) - 1;
    const uint64_t MASK31 = (1uLL << 31) - 1;
    const uint64_t MOD = (1uLL << 61) - 1;
    const uint64_t POSITIVIZER = MOD * ((1uLL << 3) - 1);
    static uint32_t Base;
    vector<uint64_t> powMemo;
    vector<uint64_t> hash;

    RollingHash(const string &s) {
        int n = (int)s.size();
        hash.assign(n+1, 0);
        powMemo.assign(n+1, 1);

        for (int i = 0; i < n; i++) {
            hash[i+1] = CalcMod(Mul(hash[i], Base) + s[i]);
            powMemo[i+1] = CalcMod(Mul(powMemo[i], Base));
        }
    }

    inline uint64_t Slice(int start, int len) {
        return CalcMod(hash[start + len] + POSITIVIZER - Mul(hash[start], powMemo[len]));
    }

    inline uint64_t CalcMod(uint64_t val) {
        val = (val & MOD) + (val >> 61);
        if (val > MOD) val -= MOD;
        return val;
    }

    inline uint64_t Mul(uint64_t a, uint64_t b) {
        uint64_t au = a >> 31;
        uint64_t ad = a & MASK31;
        uint64_t bu = b >> 31;
        uint64_t bd = b & MASK31;
        uint64_t mid = ad * bu + au * bd;
        uint64_t midu = mid >> 30;
        uint64_t midd = mid & MASK30;
        return ((au * bu) << 1) + midu + (midd << 31) + ad * bd;
    }
};

uint32_t RollingHash::Base = rand_dist(engine);


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

    RollingHash rh(s);

    int ans = 0;
    
    rep(i, m) {
        string c; cin >> c;
        RollingHash rh_c(c);
        for (int j = 0; j < (int)s.size() - (int)c.size() + 1; j++) {
            if (rh.Slice(j, c.size()) == rh_c.hash[c.size()]) ans++;
        }
    }
    
    cout << ans << endl;

    return 0;
}
0