結果

問題 No.430 文字列検索
ユーザー Manuel1024Manuel1024
提出日時 2023-01-10 18:31:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 841 ms / 2,000 ms
コード長 2,547 bytes
コンパイル時間 1,262 ms
コンパイル使用メモリ 98,604 KB
実行使用メモリ 9,028 KB
最終ジャッジ日時 2023-08-23 10:16:17
合計ジャッジ時間 10,423 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 837 ms
7,980 KB
testcase_02 AC 837 ms
8,988 KB
testcase_03 AC 836 ms
8,968 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 21 ms
4,688 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 835 ms
8,924 KB
testcase_12 AC 840 ms
8,968 KB
testcase_13 AC 837 ms
8,876 KB
testcase_14 AC 835 ms
9,028 KB
testcase_15 AC 840 ms
8,884 KB
testcase_16 AC 837 ms
8,952 KB
testcase_17 AC 841 ms
9,028 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 MOD1 = 998244353;
    static constexpr ll MOD2 = 1000000007;

    const ll b1, b2;
    const vector<ll> hs1, hs2;
    const vector<ll> rev1, rev2;
    RollingHash(const string &s): b1(rnd()), b2(rnd()),
    hs1(hash(s, b1, MOD1)), hs2(hash(s, b2, MOD2)),
    rev1(make_revlist(s.size(), b1, MOD1)), rev2(make_revlist(s.size(), b2, MOD2)){}
    RollingHash(const string &s, ll b1, ll b2): b1(b1), b2(b2),
    hs1(hash(s, b1, MOD1)), hs2(hash(s, b2, MOD2)),
    rev1(make_revlist(s.size(), b1, MOD1)), rev2(make_revlist(s.size(), b2, MOD2)){}

    // hash1(s[l, r)), hash2(s[l, r))
    pair<ll, ll> get(int l, int r){
        return {
            (hs1[r]-hs1[l]+MOD1)%MOD1*rev1[l]%MOD1,
            (hs2[r]-hs2[l]+MOD2)%MOD2*rev2[l]%MOD2
        };
    }

    vector<ll> hash(const string &s, const ll base, const ll mod){
        vector<ll> hs(s.size()+1);
        ll bm1 = 1;
        for(int i = 0; i < s.size(); i++){
            hs[i+1] = (hs[i]+bm1*s[i])%mod;
            bm1 *= base;
            bm1 %= mod;
        }
        return hs;
    }
private:
    static constexpr int bmin = 300;
    
    // 逆元の前計算
    vector<ll> make_revlist(int n, const ll base, const ll mod){
        vector<ll> rev(n);
        ll bm = 1;
        for(int i = 0; i < n; i++){
            rev[i] = mpow(bm, mod-2, mod);
            bm *= base;
            bm %= mod;
        }
        return rev;
    }
    ll rnd(){
        mt19937 mt{1UL * chrono::steady_clock::now().time_since_epoch().count()};
        return mt()%(MOD1-bmin)+bmin;
    }
    
    ll mpow(ll a, ll x, const ll mod){
        ll res = 1;
        for(; x > 0; x >>= 1){
            if(x & 1){
                res *= a;
                res %= mod;
            }
            a *= a;
            a %= mod;
        }
        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);
    vector<RollingHash> hc;
    for(auto &it: c) hc.emplace_back(it, hs.b1, hs.b2);

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