Hash.cpp 496 B

123456789101112131415
  1. unsigned long long H[maxn], xp[maxn];
  2. unsigned long long hash[maxn];
  3. //We can choose different value of x to double-check our hash function.
  4. const int x = 123;
  5. //using unsigned long long <-> % (1<<64)
  6. //s: original string
  7. void hash() {
  8. H[n] = 0;
  9. for(int i = n-1; i >= 0; i--) H[i] = H[i+1] * x + (s[i] - 'a');
  10. xp[0] = 1;
  11. for(int i = 1; i <= n; i++) xp[i] = xp[i-1] * x;
  12. //the hash value of the S[i]..S[i+L-1].
  13. //Hash(i,L) = H(i)-H(i+L)x^L
  14. // -----> hash[i] = H[i] - H[i+L]*xp[L];
  15. }