Nyaan's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View on GitHub

:heavy_check_mark: 二次元Rolling Hash
(string/rolling-hash-2d.hpp)

二次元Rolling Hash

二次元の列の一致判定を前計算$\mathrm{O}(n)$クエリ$\mathrm{O}(1)$で処理するライブラリ。

使い方

Depends on

Verified with

Code

#pragma once

#include <string>
#include <vector>
using namespace std;

#include "../internal/internal-hash.hpp"

template <typename Str, int BASE_NUM = 2>
struct RollingHash2D {
  using Hash = internal::Hash<BASE_NUM>;
  using u64 = unsigned long long;
  vector<Str> data;
  vector<vector<Hash>> hs;
  vector<Hash> pw[2];
  int h, w;
  static Hash basis[2];

  RollingHash2D(const vector<Str> &S = vector<Str>()) { build(S); }

  void build(const vector<Str> &S) {
    data = S;
    h = S.size();
    w = S[0].size();
    pw[0].resize(h + 1);
    pw[1].resize(w + 1);
    pw[0][0] = pw[1][0] = Hash::set(1);
    for (int i = 1; i <= h; i++) pw[0][i] = pw[0][i - 1] * basis[0];
    for (int i = 1; i <= w; i++) pw[1][i] = pw[1][i - 1] * basis[1];
    hs.resize(h + 1, vector<Hash>(w + 1));
    hs[0][0] = Hash::set(0);
    for (int i = 1; i <= h; i++) {
      hs[i][0] = Hash::set(0);
      for (int j = 1; j <= w; j++)
        hs[i][j] = pfma(hs[i][j - 1], basis[1], S[i - 1][j - 1]);
    }
    for (int j = 1; j <= w; j++) {
      hs[0][j] = Hash::set(0);
      for (int i = 1; i <= h; i++)
        hs[i][j] = pfma(hs[i - 1][j], basis[0], hs[i][j]);
    }
  }

  Hash get(int u, int l, int d, int r) {
    return hs[d][r] - hs[u][r] * pw[0][d - u] - hs[d][l] * pw[1][r - l] +
           hs[u][l] * pw[0][d - u] * pw[1][r - l];
  }

  static Hash get_hash(const vector<Str> &T) {
    Hash ret = Hash::set(0);
    for (int i = 0; i < (int)T.size(); i++) {
      Hash h = Hash::set(0);
      for (int j = 0; j < (int)T[0].size(); j++) h = pfma(h, basis[1], T[i][j]);
      ret = pfma(ret, basis[0], h);
    }
    return ret;
  }
};

template <typename Str, int BASE_NUM>
typename RollingHash2D<Str, BASE_NUM>::Hash
    RollingHash2D<Str, BASE_NUM>::basis[2] = {
        internal::Hash<BASE_NUM>::get_basis(),
        internal::Hash<BASE_NUM>::get_basis()};
using roriha2d = RollingHash2D<string, 1>;

/**
 * @brief 二次元Rolling Hash
 * @docs docs/string/rolling-hash-2d.md
 */
#line 2 "string/rolling-hash-2d.hpp"

#include <string>
#include <vector>
using namespace std;

#line 2 "internal/internal-hash.hpp"

namespace internal {
using i64 = long long;
using u64 = unsigned long long;
using u128 = __uint128_t;

template <int BASE_NUM = 2>
struct Hash : array<u64, BASE_NUM> {
  using array<u64, BASE_NUM>::operator[];
  static constexpr int n = BASE_NUM;

  Hash() : array<u64, BASE_NUM>() {}

  static constexpr u64 md = (1ull << 61) - 1;

  constexpr static Hash set(const i64 &a) {
    Hash res;
    fill(begin(res), end(res), cast(a));
    return res;
  }
  Hash &operator+=(const Hash &r) {
    for (int i = 0; i < n; i++)
      if (((*this)[i] += r[i]) >= md) (*this)[i] -= md;
    return *this;
  }
  Hash &operator+=(const i64 &r) {
    u64 s = cast(r);
    for (int i = 0; i < n; i++)
      if (((*this)[i] += s) >= md) (*this)[i] -= md;
    return *this;
  }
  Hash &operator-=(const Hash &r) {
    for (int i = 0; i < n; i++)
      if (((*this)[i] += md - r[i]) >= md) (*this)[i] -= md;
    return *this;
  }
  Hash &operator-=(const i64 &r) {
    u64 s = cast(r);
    for (int i = 0; i < n; i++)
      if (((*this)[i] += md - s) >= md) (*this)[i] -= md;
    return *this;
  }
  Hash &operator*=(const Hash &r) {
    for (int i = 0; i < n; i++) (*this)[i] = modmul((*this)[i], r[i]);
    return *this;
  }
  Hash &operator*=(const i64 &r) {
    u64 s = cast(r);
    for (int i = 0; i < n; i++) (*this)[i] = modmul((*this)[i], s);
    return *this;
  }

  Hash operator+(const Hash &r) { return Hash(*this) += r; }
  Hash operator+(const i64 &r) { return Hash(*this) += r; }
  Hash operator-(const Hash &r) { return Hash(*this) -= r; }
  Hash operator-(const i64 &r) { return Hash(*this) -= r; }
  Hash operator*(const Hash &r) { return Hash(*this) *= r; }
  Hash operator*(const i64 &r) { return Hash(*this) *= r; }
  Hash operator-() const {
    Hash res;
    for (int i = 0; i < n; i++) res[i] = (*this)[i] == 0 ? 0 : md - (*this)[i];
    return res;
  }
  friend Hash pfma(const Hash &a, const Hash &b, const Hash &c) {
    Hash res;
    for (int i = 0; i < n; i++) res[i] = modfma(a[i], b[i], c[i]);
    return res;
  }
  friend Hash pfma(const Hash &a, const Hash &b, const i64 &c) {
    Hash res;
    u64 s = cast(c);
    for (int i = 0; i < n; i++) res[i] = modfma(a[i], b[i], s);
    return res;
  }

  Hash pow(long long e) {
    Hash a{*this}, res{Hash::set(1)};
    for (; e; a *= a, e >>= 1) {
      if (e & 1) res *= a;
    }
    return res;
  }

  static Hash get_basis() {
    static auto rand_time =
        chrono::duration_cast<chrono::nanoseconds>(
            chrono::high_resolution_clock::now().time_since_epoch())
            .count();
    static mt19937_64 rng(rand_time);
    Hash h;
    for (int i = 0; i < n; i++) {
      while (isPrimitive(h[i] = rng() % (md - 1) + 1) == false)
        ;
    }
    return h;
  }

 private:
  static u64 modpow(u64 a, u64 b) {
    u64 r = 1;
    for (a %= md; b; a = modmul(a, a), b >>= 1) r = modmul(r, a);
    return r;
  }
  static bool isPrimitive(u64 x) {
    for (auto &d : vector<u64>{2, 3, 5, 7, 11, 13, 31, 41, 61, 151, 331, 1321})
      if (modpow(x, (md - 1) / d) <= 1) return false;
    return true;
  }
  static inline constexpr u64 cast(const long long &a) {
    return a < 0 ? a + md : a;
  }
  static inline constexpr u64 modmul(const u64 &a, const u64 &b) { 
    u128 d = u128(a) * b;
    u64 ret = (u64(d) & md) + u64(d >> 61);
    return ret >= md ? ret - md : ret;
  }
  static inline constexpr u64 modfma(const u64 &a, const u64 &b, const u64 &c) {
    u128 d = u128(a) * b + c;
    u64 ret = (d >> 61) + (u64(d) & md);
    return ret >= md ? ret - md : ret;
  }
};

}  // namespace internal

/**
 * @brief ハッシュ構造体
 * @docs docs/internal/internal-hash.md
 */
#line 8 "string/rolling-hash-2d.hpp"

template <typename Str, int BASE_NUM = 2>
struct RollingHash2D {
  using Hash = internal::Hash<BASE_NUM>;
  using u64 = unsigned long long;
  vector<Str> data;
  vector<vector<Hash>> hs;
  vector<Hash> pw[2];
  int h, w;
  static Hash basis[2];

  RollingHash2D(const vector<Str> &S = vector<Str>()) { build(S); }

  void build(const vector<Str> &S) {
    data = S;
    h = S.size();
    w = S[0].size();
    pw[0].resize(h + 1);
    pw[1].resize(w + 1);
    pw[0][0] = pw[1][0] = Hash::set(1);
    for (int i = 1; i <= h; i++) pw[0][i] = pw[0][i - 1] * basis[0];
    for (int i = 1; i <= w; i++) pw[1][i] = pw[1][i - 1] * basis[1];
    hs.resize(h + 1, vector<Hash>(w + 1));
    hs[0][0] = Hash::set(0);
    for (int i = 1; i <= h; i++) {
      hs[i][0] = Hash::set(0);
      for (int j = 1; j <= w; j++)
        hs[i][j] = pfma(hs[i][j - 1], basis[1], S[i - 1][j - 1]);
    }
    for (int j = 1; j <= w; j++) {
      hs[0][j] = Hash::set(0);
      for (int i = 1; i <= h; i++)
        hs[i][j] = pfma(hs[i - 1][j], basis[0], hs[i][j]);
    }
  }

  Hash get(int u, int l, int d, int r) {
    return hs[d][r] - hs[u][r] * pw[0][d - u] - hs[d][l] * pw[1][r - l] +
           hs[u][l] * pw[0][d - u] * pw[1][r - l];
  }

  static Hash get_hash(const vector<Str> &T) {
    Hash ret = Hash::set(0);
    for (int i = 0; i < (int)T.size(); i++) {
      Hash h = Hash::set(0);
      for (int j = 0; j < (int)T[0].size(); j++) h = pfma(h, basis[1], T[i][j]);
      ret = pfma(ret, basis[0], h);
    }
    return ret;
  }
};

template <typename Str, int BASE_NUM>
typename RollingHash2D<Str, BASE_NUM>::Hash
    RollingHash2D<Str, BASE_NUM>::basis[2] = {
        internal::Hash<BASE_NUM>::get_basis(),
        internal::Hash<BASE_NUM>::get_basis()};
using roriha2d = RollingHash2D<string, 1>;

/**
 * @brief 二次元Rolling Hash
 * @docs docs/string/rolling-hash-2d.md
 */
Back to top page