結果
| 問題 | No.430 文字列検索 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-10-13 14:40:03 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                TLE
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,731 bytes | 
| コンパイル時間 | 1,795 ms | 
| コンパイル使用メモリ | 185,648 KB | 
| 実行使用メモリ | 6,824 KB | 
| 最終ジャッジ日時 | 2024-11-10 00:48:20 | 
| 合計ジャッジ時間 | 24,564 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 4 TLE * 10 | 
ソースコード
#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;
}
            
            
            
        