Advertisement
Mlxa

### Прямой и обратный хеш по модулю 2^64

Feb 17th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define long ll
  5. #define all(x) begin(x), end(x)
  6.  
  7. const int N = 1e6;
  8. const int P = 257;
  9. uint64_t p_pow[N];
  10.  
  11. struct Initialization {
  12.     Initialization() {
  13.         p_pow[0] = 1;
  14.         for (int i = 1; i < N; ++i) {
  15.             p_pow[i] = p_pow[i - 1] * P;
  16.         }
  17.     }
  18. } init;
  19.  
  20. struct Forward_Binary_Hash {
  21.     int n;
  22.     string s;
  23.     vector<uint64_t> h;
  24.    
  25.     Forward_Binary_Hash(const string &from) :
  26.         n((int)from.size()),
  27.         s(from),
  28.         h(1, 0) {
  29.         h.reserve(n + 1);
  30.         for (char c : s) {
  31.             h.push_back(h.back() * P + c);
  32.         }
  33.     }
  34.    
  35.     uint64_t operator()(int l, int r) {
  36.         //assert(0 <= l && l <= r && r <= n - 1);
  37.         return h[r + 1] - h[l] * p_pow[r - l + 1];
  38.     }
  39. };
  40.  
  41. struct Backward_Binary_Hash {
  42.     int n;
  43.     string s;
  44.     vector<uint64_t> h;
  45.    
  46.     Backward_Binary_Hash(const string &from) :
  47.         n((int)from.size()),
  48.         s(from),
  49.         h(1, 0) {
  50.         h.reserve(n + 1);
  51.         reverse(all(s));
  52.         for (char c : s) {
  53.             h.push_back(h.back() * P + c);
  54.         }
  55.     }
  56.    
  57.     uint64_t operator()(int revl, int revr) {
  58.         //assert(0 <= revl && revl <= revr && revr <= n - 1);
  59.         int l = n - 1 - revr, r = n - 1 - revl;
  60.         //assert(0 <= l && l <= r && r <= n - 1);
  61.         return h[r + 1] - h[l] * p_pow[r - l + 1];
  62.     }
  63. };
  64.  
  65. int main() {
  66. #ifdef LC
  67.     assert(freopen("input.txt", "r", stdin));
  68. #endif
  69.     ios::sync_with_stdio(false);
  70.     cin.tie(nullptr);
  71.    
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement