結果
| 問題 | No.430 文字列検索 | 
| コンテスト | |
| ユーザー |  Manuel1024 | 
| 提出日時 | 2023-01-10 06:53:01 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,191 ms / 2,000 ms | 
| コード長 | 2,619 bytes | 
| コンパイル時間 | 917 ms | 
| コンパイル使用メモリ | 97,968 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-11-10 01:04:29 | 
| 合計ジャッジ時間 | 13,344 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 14 | 
ソースコード
#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))
    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
    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
    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;
    }
    ll rnd(){
        mt19937_64 mt{1ULL * chrono::steady_clock::now().time_since_epoch().count()};
        return mt()%(MOD-bmin)+bmin;
    }
    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;
}
            
            
            
        