結果

問題 No.430 文字列検索
ユーザー risujirohrisujiroh
提出日時 2019-03-14 14:55:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 2,467 bytes
コンパイル時間 2,000 ms
コンパイル使用メモリ 178,880 KB
実行使用メモリ 5,224 KB
最終ジャッジ日時 2023-09-08 18:03:09
合計ジャッジ時間 3,027 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 62 ms
4,996 KB
testcase_02 AC 16 ms
4,928 KB
testcase_03 AC 15 ms
4,936 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 17 ms
4,944 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 44 ms
5,060 KB
testcase_12 AC 44 ms
4,936 KB
testcase_13 AC 44 ms
5,124 KB
testcase_14 AC 39 ms
4,936 KB
testcase_15 AC 33 ms
4,940 KB
testcase_16 AC 17 ms
5,056 KB
testcase_17 AC 16 ms
5,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

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 constexpr uint64_t P0 = 4e9 + 7;
  static constexpr uint64_t P1 = 4e9 + 9;
  static uint64_t B0;
  static uint64_t B1;
  static V<uint64_t> powB0;
  static V<uint64_t> powB1;
  const int n;
  V<uint64_t> h0;
  V<uint64_t> 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) % P0;
      h1[i + 1] = (h1[i] * B1 + *first) % P1;
    }
    while (powB0.size() <= n) {
      powB0.push_back(powB0.back() * B0 % P0);
      powB1.push_back(powB1.back() * B1 % P1);
    }
  }
  uint64_t get0(int l, int r) { return (h0[r] + (P0 - h0[l]) * powB0[r - l]) % P0; }
  uint64_t get1(int l, int r) { return (h1[r] + (P1 - h1[l]) * powB1[r - l]) % P1; }
  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 uint64_t get0(Itr first, Itr last) {
    uint64_t res = 0;
    while (first != last) {
      res = (res * B0 + *first++) % P0;
    }
    return res;
  }
  template<class Itr> static uint64_t get1(Itr first, Itr last) {
    uint64_t res = 0;
    while (first != last) {
      res = (res * B1 + *first++) % P1;
    }
    return res;
  }
};
uint64_t RollingHash::B0 = rng<unsigned>(1, RollingHash::P0);
uint64_t RollingHash::B1 = rng<unsigned>(1, RollingHash::P1);
V<uint64_t> RollingHash::powB0{1};
V<uint64_t> RollingHash::powB1{1};

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  string s; cin >> s;
  int n = s.size();
  RollingHash rh(begin(s), end(s));
  int m; cin >> m;
  map< pair<unsigned, unsigned>, int > mp;
  while (m--) {
    string c; cin >> c;
    unsigned h0 = RollingHash::get0(begin(c), end(c));
    unsigned h1 = RollingHash::get1(begin(c), end(c));
    ++mp[{h0, h1}];
  }
  int res = 0;
  for (int w = 1; w <= 10; ++w) {
    for (int i = 0; i + w <= n; ++i) {
      unsigned h0 = rh.get0(i, i + w);
      unsigned h1 = rh.get1(i, i + w);
      if (!mp.count({h0, h1})) continue;
      res += mp[{h0, h1}];
    }
  }
  cout << res << '\n';
}
0