結果

問題 No.430 文字列検索
ユーザー Manuel1024Manuel1024
提出日時 2023-01-10 01:51:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 837 ms / 2,000 ms
コード長 2,971 bytes
コンパイル時間 995 ms
コンパイル使用メモリ 96,496 KB
実行使用メモリ 7,416 KB
最終ジャッジ日時 2023-08-23 04:24:16
合計ジャッジ時間 10,428 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 837 ms
6,788 KB
testcase_02 AC 833 ms
7,312 KB
testcase_03 AC 837 ms
7,416 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 21 ms
4,668 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 5 ms
4,376 KB
testcase_11 AC 828 ms
7,352 KB
testcase_12 AC 837 ms
7,256 KB
testcase_13 AC 835 ms
7,364 KB
testcase_14 AC 833 ms
7,412 KB
testcase_15 AC 833 ms
7,360 KB
testcase_16 AC 837 ms
7,308 KB
testcase_17 AC 836 ms
7,316 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: メンバ関数 ‘ll RollingHash::rnd()’ 内:
main.cpp:65:72: 警告: narrowing conversion of ‘std::chrono::_V2::steady_clock::now().std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >::time_since_epoch().std::chrono::duration<long int, std::ratio<1, 1000000000> >::count()’ from ‘std::chrono::duration<long int, std::ratio<1, 1000000000> >::rep’ {aka ‘long int’} to ‘std::mersenne_twister_engine<long unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>::result_type’ {aka ‘long unsigned int’} [-Wnarrowing]
   65 |         mt19937 mt{chrono::steady_clock::now().time_since_epoch().count()};
      |                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~

ソースコード

diff #

/*
    文字列 S の RollingHash を扱う構造体
    hash(s[0, i)) := s_0*b^0 + s_1*b^1 + ... + s_(i-1)*b^(i-1)
    b は基数 [2, MOD) から乱択
    32bit の mod 2つver

    RollingHash hs(s) 文字列 s の RollingHashを構築 基数 b1, b2 は乱択
    RollingHash hs(s, b1, b2) 文字列 s の RollingHashを構築 基数 b1, b2 はこちらで指定

    get(l, r) s[l, r) の hash を取得
*/
#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;
    vector<ll> hs1, hs2;
    vector<ll> rev1, rev2;
    RollingHash(const string &s): b1(rnd()), b2(rnd()){
        init(s);
    }
    RollingHash(const string &s, ll b1, ll b2): b1(b1), b2(b2){
        init(s);
    }

    // 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
        };
    }
private:
    void init(const string &s){
        hs1 = vector<ll>(s.size()+1);
        rev1 = vector<ll>(s.size());
        ll bm1 = 1;
        for(int i = 0; i < s.size(); i++){
            hs1[i+1] = (hs1[i]+bm1*(s[i]-'A'+1))%MOD1;
            rev1[i] = mpow1(bm1, MOD1-2);
            bm1 *= b1;
            bm1 %= MOD1;
        }

        hs2 = vector<ll>(s.size()+1);
        rev2 = vector<ll>(s.size());
        ll bm2 = b2;
        for(int i = 0; i < s.size(); i++){
            hs2[i+1] = (hs2[i]+bm2*(s[i]-'A'+1))%MOD2;
            rev2[i] = mpow2(bm2, MOD2-2);
            bm2 *= b2;
            bm2 %= MOD2;
        }
    }
    ll rnd(){
        mt19937 mt{chrono::steady_clock::now().time_since_epoch().count()};
        return mt()%(MOD1-2)+2;
    }
    ll mpow1(ll a, ll x){
        ll res = 1;
        for(; x > 0; x >>= 1){
            if(x & 1){
                res *= a;
                res %= MOD1;
            }
            a *= a;
            a %= MOD1;
        }
        return res;
    }
    ll mpow2(ll a, ll x){
        ll res = 1;
        for(; x > 0; x >>= 1){
            if(x & 1){
                res *= a;
                res %= MOD2;
            }
            a *= a;
            a %= MOD2;
        }
        return res;
    }
};

// https://yukicoder.me/problems/no/430
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