Template

转至文末

字符串双模哈希

P11276 第一首歌

Base Mod
\(137\) \(10^9+7\)
\(139\) \(10^9+9\)
struct Hash {
  ll base, mod, p[N], hs[N];
  Hash(ll b, ll n, ll m) {
    base = b, mod = m, p[0] = 1;
    for (ll i = 1; i <= n; i++) p[i] = p[i - 1] * base % mod;
  }
  void make(string s) {
    ll n = s.size();
    s = '0' + s, hs[0] = 0;
    for (ll i = 1; i <= n; i++) hs[i] = (hs[i - 1] * base % mod + s[i]) % mod;
  }
  ll get(ll l, ll r) {
    return (hs[r] + mod - hs[l - 1] * p[r - l + 1] % mod) % mod;
  }
} hs1(137, 1e6, 1e9 + 7), hs2(139, 1e6, 1e9 + 9);

posted on 2024-11-18 23:51  UXOD  阅读(5)  评论(0编辑  收藏  举报