結果

問題 No.430 文字列検索
ユーザー kyo1kyo1
提出日時 2020-06-04 15:23:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 763 ms / 2,000 ms
コード長 2,302 bytes
コンパイル時間 1,985 ms
コンパイル使用メモリ 202,544 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-19 11:39:21
合計ジャッジ時間 10,682 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

class Random {
 private:
  static inline std::mt19937 mt{std::random_device()()};

 public:
  template <typename T>
  static T random(T hi) {
    std::uniform_int_distribution<T> uid(0, hi);
    return uid(mt);
  }

  template <typename T>
  static T random(T lo, T hi) {
    std::uniform_int_distribution<T> uid(lo, hi);
    return uid(mt);
  }
};

class RollingHash {
 private:
  const uint64_t mod = (UINT64_C(1) << 61) - 1;
  static inline uint64_t base =
      Random::random(UINT64_C(10007), UINT64_C(1) << 28);
  std::vector<uint64_t> hashes, pows;

  uint64_t add(uint64_t a, uint64_t b) const {
    a += b;
    if (a >= mod) a -= mod;
    return a;
  }

  uint64_t sub(uint64_t a, uint64_t b) const {
    if (a < b) a += mod;
    a -= b;
    return a;
  }

  uint64_t mul(uint64_t a, uint64_t b) const {
    uint64_t ah = a >> 31, al = a & ((UINT64_C(1) << 31) - 1), bh = b >> 31,
             bl = b & ((UINT64_C(1) << 31) - 1), m = ah * bl + al * bh,
             v = (ah * bh << 1) + (m >> 30) +
                 ((m & ((UINT64_C(1) << 30) - 1)) << 31) + al * bl;
    v = (v & ((UINT64_C(1) << 61) - 1)) + (v >> 61);
    return v;
  }

 public:
  template <typename T>
  RollingHash(const T &S) : hashes(S.size() + 1, 0), pows(S.size() + 1, 0) {
    pows[0] = 1;
    for (size_t i = 0; i < S.size(); i++) {
      pows[i + 1] = mul(pows[i], base);
      hashes[i + 1] = add(mul(hashes[i], base), S[i]);
    }
  }

  template <typename T>
  RollingHash(const T &S, const uint64_t base)
      : hashes(S.size() + 1, 0), pows(S.size() + 1, 0) {
    pows[0] = 1;
    for (size_t i = 0; i < S.size(); i++) {
      pows[i + 1] = mul(pows[i], base);
      hashes[i + 1] = add(mul(hashes[i], base), S[i]);
    }
  }

  // S[l, r)
  uint64_t slice(int l, int r) const {
    return sub(hashes[r], mul(hashes[l], pows[r - l]));
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string S;
  cin >> S;
  int M;
  cin >> M;
  RollingHash rhs(S);
  int res = 0;
  for (int i = 0; i < M; i++) {
    string C;
    cin >> C;
    RollingHash rhc(C);
    for (int j = 0; j + (int)C.size() < (int)S.size() + 1; j++) {
      if (rhs.slice(j, j + (int)C.size()) == rhc.slice(0, (int)C.size())) res++;
    }
  }
  cout << res << '\n';
  return 0;
}
0