結果

問題 No.430 文字列検索
ユーザー Manuel1024Manuel1024
提出日時 2023-01-10 06:57:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,308 ms / 2,000 ms
コード長 2,654 bytes
コンパイル時間 938 ms
コンパイル使用メモリ 91,908 KB
実行使用メモリ 4,468 KB
最終ジャッジ日時 2023-08-23 07:19:45
合計ジャッジ時間 15,110 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1,308 ms
4,376 KB
testcase_02 AC 1,306 ms
4,376 KB
testcase_03 AC 1,305 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 35 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 7 ms
4,376 KB
testcase_11 AC 1,304 ms
4,380 KB
testcase_12 AC 1,303 ms
4,376 KB
testcase_13 AC 1,300 ms
4,376 KB
testcase_14 AC 1,303 ms
4,468 KB
testcase_15 AC 1,302 ms
4,380 KB
testcase_16 AC 1,306 ms
4,376 KB
testcase_17 AC 1,304 ms
4,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <random>
#include <chrono>
using namespace std;
using ll = long long;

struct RollingHash{
    static constexpr ll MOD = (1LL << 61)-1;

    const ll b1;
    const vector<ll> hs;
    const vector<ll> rev;
    RollingHash(const string &s): b1(rnd()), hs(hash(s)), rev(make_revlist(s.size())){}
    RollingHash(const string &s, ll b0): b1(b0), hs(hash(s)), rev(make_revlist(s.size())){}

    // hash(s[l, r))
    inline ll get(int l, int r){
        return Mul(calcMod(MOD+hs[r]-hs[l]), rev[l]);//(hs[r]-hs[l]+MOD)%MOD*rev[l]%MOD
    }

    vector<ll> hash(const string &s){
        vector<ll> hs(s.size()+1);
        hs[0] = 0;
        ll bm = 1;
        for(int i = 0; i < s.size(); i++){
            hs[i+1] = calcMod(hs[i]+Mul(bm, s[i]));
            bm = Mul(bm, b1);
        }
        return hs;
    }
private:
    static constexpr ll MASK31 = (1LL << 31)-1;
    static constexpr ll MASK30 = (1LL << 30)-1;
    static constexpr ll MASK61 = (1LL << 61)-1;
    static constexpr int bmin = 300;
    // a*b mod 2^61-1
    inline ll Mul(const ll a, const ll b){
        ll au = a >> 31;
        ll ad = a & MASK31;
        ll bu = b >> 31;
        ll bd = b & MASK31;
        ll mid = ad*bu + au*bd;
        ll midu = mid >> 30;
        ll midd = mid & MASK30;
        return calcMod(au*bu*2 + midu + (midd << 31) + ad*bd);
    }

    // x mod 2^61-1
    inline ll calcMod(ll x){
        ll xu = x >> 61;
        ll xd = x & MASK61;
        ll res = xu+xd;
        if(res >= MOD) res -= MOD;
        return res;
    }

    // 逆元の前計算
    vector<ll> make_revlist(int n){
        vector<ll> rev(n);
        ll bm = 1;
        for(int i = 0; i < n; i++){
            rev[i] = mpow(bm, MOD-2);
            bm = Mul(bm, b1);
        }
        return rev;
    }
    inline ll rnd(){
        mt19937_64 mt{1ULL * chrono::steady_clock::now().time_since_epoch().count()};
        return mt()%(MOD-bmin)+bmin;
    }
    inline ll mpow(ll a, ll x){
        ll res = 1;
        for(; x > 0; x >>= 1){
            if(x & 1){
                res = Mul(res, a);
            }
            a = Mul(a, a);
        }
        return res;
    }
};

int main(){
    string s; cin >> s;
    int m; cin >> m;
    vector<string> c(m);
    for(auto &it: c) cin >> it;

    RollingHash hs(s);

    ll ans = 0;
    for(int i = 0; i < m; i++){
        auto hc = hs.hash(c[i]);
        for(int j = 0; j+c[i].size() <= s.size(); j++){
            if(hs.get(j, j+c[i].size()) == hc[c[i].size()]){
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}
0