結果

問題 No.430 文字列検索
ユーザー kyort0nkyort0n
提出日時 2019-03-14 14:10:24
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,440 bytes
コンパイル時間 1,547 ms
コンパイル使用メモリ 151,940 KB
実行使用メモリ 9,484 KB
最終ジャッジ日時 2023-09-08 16:50:07
合計ジャッジ時間 4,929 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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;
typedef long long ll;
typedef pair<ll, ll> l_l;
template<class Z> Z rng(Z a, Z b) {
  static mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
  return uniform_int_distribution<Z>(a, b - 1)(mt);
}

struct RollingHash {
  static ll mod0;
  static ll mod1;
  static ll B0;
  static ll B1;
  static vector<ll> powB0;
  static vector<ll> powB1;
  const int n;
  vector<ll> h0;
  vector<ll> h1;
  template<class Itr> RollingHash(Itr first, Itr last) :
    n(distance(first, last)), h0(n + 1), h1(n + 1) {
    for (int i = 0; i < n; ++i, ++first) {
      h0[i + 1] = (h0[i] * B0 + *first) % mod0;
      h1[i + 1] = (h1[i] * B1 + *first) % mod1;
    }
    while (powB0.size() <= n) {
      powB0.push_back(powB0.back() * B0 % mod0);
      powB1.push_back(powB1.back() * B1 % mod1);
    }
  }
  ll get0(int l, int r) { return (h0[r] - h0[l] * powB0[r - l] % mod0 + mod0) % mod0; }
  ll get1(int l, int r) { return (h1[r] - h1[l] * powB1[r - l] % mod1 + mod1) % mod1; }
  bool eq(int l0, int r0, int l1, int r1) {
    return get0(l0, r0) == get0(l1, r1) and get1(l0, r0) == get1(l1, r1);
  }
  template<class Itr> static ll get0(Itr first, Itr last) {
    ll res = 0;
    while (first != last) {
      res = (res * B0 + *first++) % mod0;
    }
    return res;
  }
  template<class Itr> static ll get1(Itr first, Itr last) {
    ll res = 0;
    while (first != last) {
      res = (res * B1 + *first++) % mod1;
    }
    return res;
  }

  l_l get(int l, int r) {
    return {get0(l, r), get1(l, r)};
  }
};

ll RollingHash::mod0 = 1e9+7;
ll RollingHash::mod1 = 998244353;
ll RollingHash::B0 = rng<ll>(1e8, 1e9);
ll RollingHash::B1 = rng<ll>(1e8, 1e9);
vector<ll> RollingHash::powB0{1};
vector<ll> RollingHash::powB1{1};


#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll M;
    string S;
    cin >> S >> M;
    ll ans = 0;
    RollingHash rhs(S.begin(), S.end());
    l_l NowHash;
    string C;
    while(M--) {
        cin >> C;
        NowHash.first = RollingHash::get0(C.begin(), C.end());
        NowHash.second = RollingHash::get1(C.begin(), C.end());
        int SIZE = C.size();
        for(int i = 0; i + SIZE <= S.size(); i++) {
            if(rhs.get(i, i + SIZE) == NowHash) ans++;
        }
    }
    cout << ans << endl;
    return 0;
}
0