結果

問題 No.430 文字列検索
ユーザー simkarensimkaren
提出日時 2020-10-13 14:38:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,682 bytes
コンパイル時間 1,753 ms
コンパイル使用メモリ 169,888 KB
実行使用メモリ 9,308 KB
最終ジャッジ日時 2023-09-28 00:04:27
合計ジャッジ時間 5,627 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,756 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define ll long long

template<int n_mod = 2>
struct RollingHash{
    using hash_arr = std::array<ll, n_mod>;
    const int base = 1009;
    const ll p_mod[4] = {1000000009LL, 1000000007LL, 998244353LL, 469762049LL};

    std::string text;
    int len;
    vector<hash_arr> pw, hs;

    // constructor
    RollingHash(string& S){
        static_assert(1 <= n_mod && n_mod <= 4);
        text = S; len = S.length();
        pw.resize(len + 1);
        hs.resize(len + 1);
        for (int j = 0; j < n_mod; ++j)
            hs[0].at(j) = 0LL, pw[0].at(j) = 1LL;
        for (int i = 0; i < len; ++i){
            for (int j = 0; j < n_mod; ++j){
                hs[i + 1].at(j) = (hs[i].at(j) + text[i]) * base % p_mod[j];
                pw[i + 1].at(j) = pw[i].at(j) * base % p_mod[j];
            }
        }
    } 

    // text[l, r)のハッシュ値を計算する
    hash_arr get_hash(int l, int r){
        hash_arr ret;
        for (int j = 0; j < n_mod; ++j){
            ret.at(j) = hs[r].at(j) - hs[l].at(j) * pw[r - l].at(j) % p_mod[j];
            if (ret.at(j) < 0) ret.at(j) += p_mod[j];
        }
        return ret;
    }

    // text全体のハッシュ値を計算する
    hash_arr get(void){ return hs[len]; }
};

int main(void){
    string S; cin >> S;
    RollingHash<> rh(S);
    int M; cin >> M;
    int ans = 0;
    while (M--){
        string C; cin >> C;
        RollingHash<> _rh(C);
        auto hs = _rh.get();
        for (int i = 0; i <= (int)(S.length() - C.length()); ++i)
            if (hs == rh.get_hash(i, i + C.length()))
                ++ans;
    }
    cout << ans << endl;
    return 0;
}
0