結果

問題 No.430 文字列検索
ユーザー yuppe19 😺yuppe19 😺
提出日時 2020-08-17 10:49:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 224 ms / 2,000 ms
コード長 2,550 bytes
コンパイル時間 978 ms
コンパイル使用メモリ 94,272 KB
実行使用メモリ 23,492 KB
最終ジャッジ日時 2024-04-27 05:00:10
合計ジャッジ時間 2,411 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 224 ms
23,468 KB
testcase_02 AC 22 ms
6,944 KB
testcase_03 AC 24 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 200 ms
23,492 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 9 ms
6,944 KB
testcase_11 AC 41 ms
6,944 KB
testcase_12 AC 42 ms
7,064 KB
testcase_13 AC 42 ms
6,940 KB
testcase_14 AC 36 ms
6,940 KB
testcase_15 AC 32 ms
6,944 KB
testcase_16 AC 25 ms
6,944 KB
testcase_17 AC 23 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
using u64 = uint64_t;

u64 uz = time(NULL);
inline u64 xorshift64() {
  uz ^= uz << 18;
  uz ^= uz >> 19;
  uz ^= uz << 19;
  return uz;
}

template <class T>
class rolling_hash {
  static constexpr u64 M = 0xffff'ffff'0000'0001;
  static constexpr u64 mod_add(u64 a, u64 b) {
    u64 r = a + b;
    if(r < a) { return r + 0xffff'ffffU; }
    return r < M ? r : r - M;
  }

  static constexpr u64 mod_sub(u64 a, u64 b) {
    u64 r = a - b;
    return a < b ? r + M : r;
  }

  static constexpr u64 mod_reduce(u64 b, u64 a) {
    u64 b1 = b >> 32, b0 = b & 0xffff'ffffU;
    if(a >= M) { a -= M; }
    a = mod_sub(a, b0);
    a = mod_sub(a, b1);
    a = mod_add(a, b0 << 32);
    return a;
  }

  static constexpr u64 mod_mul(u64 a, u64 b) {
    u64 a1 = a >> 32, a0 = a & 0xffff'ffffU,
        b1 = b >> 32, b0 = b & 0xffff'ffffU;
    a = a1 * b0;
    b = a0 * b1;
    u64 x0 = a0 * b0,
        x2 = a1 * b1;
    a += b;
    if(a < b) { x2 += 1UL << 32; }
    u64 t = a << 32;
    x0 += t;
    if(x0 < t) { ++x2; }
    x2 += a >> 32;
    return mod_reduce(x2, x0);
  }
  T s;
  u64 b61;
  size_t n;
  vector<u64> B, H;
 public:
  rolling_hash() {}
  ~rolling_hash() noexcept {}
  explicit rolling_hash(const T &_s, u64 seed=xorshift64()) : s(_s), b61(seed % (M-8194) + 8193), n(s.size()) {
    B.resize(n+1);
    H.resize(n+1);
    B[0] = 1;
    for(size_t i=0; i<n; ++i) {
      B[i+1] = mod_mul(B[i], b61);
      H[i+1] = mod_add(mod_mul(H[i], b61), s[i]);
    }
  }
  u64 calc_hash(size_t l, size_t r) const {    // [l, r]. つまり l も r も含む。1-indexed.
    assert(l <= r);
    assert(l <= n && r <= n); // if(l > n || r > n) { return -1; }
    return calc_hash2(l, r-l+1);
  }
  u64 calc_hash2(size_t l, size_t len) const { // l から len 文字。1-indexed.
    assert(1 <= len);
    assert(l <= n && l+len <= n+1);
    u64 x = H[l+len-1], yz = mod_mul(B[len], H[l-1]);
    return x < yz ? M - yz + x : x - yz;
  }
};

int main(void) {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  string s; int m; cin >> s >> m;
  u64 seed = xorshift64();
  rolling_hash rh(s, seed);
  unordered_map<u64, int> mp;
  for(size_t i=1, n=s.size(); i<=n; ++i) {
    for(size_t len=1; len<=10 && i+len<=n+1; ++len) {
      ++mp[rh.calc_hash2(i, len)];
    }
  }
  int res = 0;
  for(int i=0; i<m; ++i) {
    string ci; cin >> ci;
    rolling_hash rh2(ci, seed);
    res += mp[rh2.calc_hash(1, ci.size())];
  }
  cout << res << '\n';
  return 0;
}
0