The5threic1-1

Ordered_Hash-template

Sep 3rd, 2024 (edited)
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | Source Code | 0 0
  1. class ordered_hash
  2. {
  3.     public:
  4.  
  5.         vector<long long> hashi[2], p_pow[2];
  6.         //call hashing(s, 0) and hashing(s, 1) first;
  7.         int hashing(string s, int st)
  8.         {
  9.             const int p = (st ? 31 : 53);
  10.             const int m = (st ? 1000000007 : 1000000021);  
  11.             const int n = s.size();
  12.             p_pow[st].resize(n+1);
  13.             p_pow[st][0] = 1;
  14.             for(int i=1; i<(int)p_pow[st].size(); i++)
  15.             {
  16.                 p_pow[st][i] = (p_pow[st][i-1] * p) % m;
  17.             }
  18.             hashi[st].resize(n+1); hashi[st][0] = 0;
  19.             for(int i=0; i<n; i++)
  20.             {
  21.                 hashi[st][i+1] = ((hashi[st][i]*p)%m + (s[i]-'a'+1))%m;
  22.             }
  23.             return 0;
  24.         }
  25.         pair<int, int> get_hash(int i, int j)
  26.         {
  27.             const int m2 = 1000000007, m1 = 1000000021;
  28.             return make_pair((hashi[0][j+1] - (hashi[0][i]*p_pow[0][j-i+1])%m1 + m1)%m1,
  29.                                 (hashi[1][j+1] - (hashi[1][i]*p_pow[1][j-i+1])%m2 + m2)%m2);
  30.         }
  31.         ordered_hash(string s)
  32.         {
  33.             hashing(s, 0); hashing(s, 1);
  34.         }
  35. };
Advertisement
Add Comment
Please, Sign In to add comment