Advertisement
osipyonok

Hash

Sep 20th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.52 KB | None | 0 0
  1. struct Hash{
  2.     vector<ll> suf , pows;
  3.     ll mod;
  4.    
  5.     Hash(string & s , int base = 153 , int _mod = 1000000007){
  6.         int n = sz(s);
  7.         suf = vector<ll>(n + 1 , 0LL);
  8.         pows = vector<ll>(n + 1 , 0LL);
  9.         pows[1] = base;
  10.         mod = _mod;
  11.         repn(i , n){
  12.             suf[i] = (s[i] + suf[i + 1] * pows[1]) % mod;
  13.         }
  14.         repf(i , 2 , n + 1){
  15.             pows[i] = (base * pows[i - 1]) % mod;
  16.         }
  17.     }
  18.    
  19.     ll str(int l , int r){ // [l , r]
  20.         ll res = (suf[l] - suf[r + 1] * pows[r - l + 1]) % mod;
  21.         while(res < 0)res += mod;
  22.         return res % mod;
  23.     }
  24. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement