cpp-library

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

View the Project on GitHub shino16/cpp-library

:heavy_check_mark: str/rolling_hash.hpp

Depends on

Verified with

Code

#pragma once
#include "util/seed.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 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; }
};
Back to top page