cpp-library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub shino16/cpp-library

:heavy_check_mark: test/str/rolling_hash.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_14_B

#include "str/rolling_hash.hpp"

char T[1000001], P[10001];

int main() {
  scanf("%s%s", T, P);
  int n = strlen(T);
  int m = strlen(P);
  rolling_hash hash(T, T+n);
  auto hP = hash(P, P+m);
  rep(i, n-m+1) if (hash(i, i+m) == hP) printf("%d\n", i);
}
#line 1 "test/str/rolling_hash.test.cpp"
// competitive-verifier: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_14_B

#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "util/seed.hpp"

auto seed() {
#if defined(LOCAL) && !defined(NO_FIX_SEED)
  return 314169265258979;
#endif
  return chrono::steady_clock::now().time_since_epoch().count();
}
#line 3 "str/rolling_hash.hpp"

class rolling_hash {
 public:
  uint64_t r;
  static constexpr uint64_t MOD = (uint64_t(1) << 61) - 1;

  template <class It>
  rolling_hash(It first, It last, uint64_t r)
      : r(r), prefix(last - first + 1), power(last - first + 1) {
    prefix[0] = 0;
    power[0] = 1;
    rep(i, last - first) {
      prefix[i + 1] = reduce(mul(prefix[i], r) + uint64_t(first[i]));
      power[i + 1] = mul(power[i], r);
    }
  }
  template <class It>
  rolling_hash(It first, It last) : rolling_hash(first, last, seed()) {}

  uint64_t operator()(int l, int r) const {
    uint64_t t = prefix[r] + MOD - mul(power[r - l], prefix[l]);
    return reduce(t);
  }
  template <class It>
  uint64_t operator()(It first, It last) const {
    uint64_t t = 0;
    for (auto it = first; it != last; it++) t = reduce(mul(t, r) + *it);
    return t;
  }

 private:
  vector<uint64_t> prefix, power;
  static uint64_t mul(uint64_t a, uint64_t b) {
    __uint128_t t = __uint128_t(a) * b;
    return reduce((t >> 61) + (t & MOD));
  }
  // [0, 2 * MOD) -> [0, MOD)
  static uint64_t reduce(uint64_t a) { return a >= MOD ? a - MOD : a; }
};
#line 4 "test/str/rolling_hash.test.cpp"

char T[1000001], P[10001];

int main() {
  scanf("%s%s", T, P);
  int n = strlen(T);
  int m = strlen(P);
  rolling_hash hash(T, T+n);
  auto hP = hash(P, P+m);
  rep(i, n-m+1) if (hash(i, i+m) == hP) printf("%d\n", i);
}

Test cases

Env Name Status Elapsed Memory
g++-12 00_sample_00.in :heavy_check_mark: AC 95 ms 8 MB
g++-12 01_small_00.in :heavy_check_mark: AC 12 ms 8 MB
g++-12 01_small_01.in :heavy_check_mark: AC 11 ms 8 MB
g++-12 01_small_02.in :heavy_check_mark: AC 11 ms 8 MB
g++-12 01_small_03.in :heavy_check_mark: AC 11 ms 8 MB
g++-12 02_rand_00.in :heavy_check_mark: AC 11 ms 8 MB
g++-12 02_rand_01.in :heavy_check_mark: AC 11 ms 8 MB
g++-12 02_rand_02.in :heavy_check_mark: AC 11 ms 8 MB
g++-12 02_rand_03.in :heavy_check_mark: AC 11 ms 8 MB
g++-12 02_rand_04.in :heavy_check_mark: AC 11 ms 8 MB
g++-12 02_rand_05.in :heavy_check_mark: AC 11 ms 8 MB
g++-12 02_rand_06.in :heavy_check_mark: AC 12 ms 8 MB
g++-12 02_rand_07.in :heavy_check_mark: AC 14 ms 10 MB
g++-12 03_large_00.in :heavy_check_mark: AC 11 ms 8 MB
g++-12 03_large_01.in :heavy_check_mark: AC 11 ms 8 MB
g++-12 03_large_02.in :heavy_check_mark: AC 14 ms 10 MB
g++-12 03_large_03.in :heavy_check_mark: AC 14 ms 10 MB
g++-12 03_large_04.in :heavy_check_mark: AC 32 ms 26 MB
g++-12 03_large_05.in :heavy_check_mark: AC 32 ms 26 MB
g++-12 03_large_06.in :heavy_check_mark: AC 32 ms 26 MB
g++-12 03_large_07.in :heavy_check_mark: AC 32 ms 26 MB
g++-12 03_large_08.in :heavy_check_mark: AC 32 ms 26 MB
g++-12 03_large_09.in :heavy_check_mark: AC 32 ms 26 MB
g++-12 04_embedded_00.in :heavy_check_mark: AC 33 ms 26 MB
g++-12 04_embedded_01.in :heavy_check_mark: AC 32 ms 26 MB
g++-12 04_embedded_02.in :heavy_check_mark: AC 32 ms 26 MB
g++-12 04_embedded_03.in :heavy_check_mark: AC 32 ms 26 MB
g++-12 04_embedded_04.in :heavy_check_mark: AC 32 ms 26 MB
g++-12 04_embedded_05.in :heavy_check_mark: AC 32 ms 26 MB
g++-12 05_corner_00.in :heavy_check_mark: AC 12 ms 8 MB
g++-12 05_corner_01.in :heavy_check_mark: AC 11 ms 8 MB
g++-12 05_corner_02.in :heavy_check_mark: AC 11 ms 8 MB
g++-12 05_corner_03.in :heavy_check_mark: AC 85 ms 26 MB
g++-12 05_corner_04.in :heavy_check_mark: AC 91 ms 26 MB
g++-12 05_corner_05.in :heavy_check_mark: AC 33 ms 26 MB
g++-12 05_corner_06.in :heavy_check_mark: AC 32 ms 26 MB
Back to top page