結果

問題 No.430 文字列検索
ユーザー simkarensimkaren
提出日時 2020-10-13 14:40:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,731 bytes
コンパイル時間 1,852 ms
コンパイル使用メモリ 181,468 KB
実行使用メモリ 4,568 KB
最終ジャッジ日時 2023-09-28 00:05:04
合計ジャッジ時間 26,796 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#pragma GCC optimize("Ofast", "unroll-loops")

#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<1> rh(S);
    int M; cin >> M;
    int ans = 0;
    while (M--){
        string C; cin >> C;
        RollingHash<1> _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