結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 917 ms
4,348 KB
testcase_02 AC 918 ms
4,348 KB
testcase_03 AC 914 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 7 ms
4,348 KB
testcase_09 RE -
testcase_10 AC 4 ms
4,348 KB
testcase_11 AC 921 ms
4,348 KB
testcase_12 AC 919 ms
4,348 KB
testcase_13 AC 915 ms
4,348 KB
testcase_14 AC 917 ms
4,348 KB
testcase_15 AC 917 ms
4,348 KB
testcase_16 AC 916 ms
4,348 KB
testcase_17 AC 916 ms
4,348 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());

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));
        }
    }

    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 = 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);
        rep(j, s.size() - c.size() + 1) {
            if (rh.Slice(j, c.size()) == rh_c.hash[c.size()]) ans++;
        }
    }
    
    cout << ans << endl;

    return 0;
}
0