Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ordered_hash
- {
- public:
- vector<long long> hashi[2], p_pow[2];
- //call hashing(s, 0) and hashing(s, 1) first;
- int hashing(string s, int st)
- {
- const int p = (st ? 31 : 53);
- const int m = (st ? 1000000007 : 1000000021);
- const int n = s.size();
- p_pow[st].resize(n+1);
- p_pow[st][0] = 1;
- for(int i=1; i<(int)p_pow[st].size(); i++)
- {
- p_pow[st][i] = (p_pow[st][i-1] * p) % m;
- }
- hashi[st].resize(n+1); hashi[st][0] = 0;
- for(int i=0; i<n; i++)
- {
- hashi[st][i+1] = ((hashi[st][i]*p)%m + (s[i]-'a'+1))%m;
- }
- return 0;
- }
- pair<int, int> get_hash(int i, int j)
- {
- const int m2 = 1000000007, m1 = 1000000021;
- return make_pair((hashi[0][j+1] - (hashi[0][i]*p_pow[0][j-i+1])%m1 + m1)%m1,
- (hashi[1][j+1] - (hashi[1][i]*p_pow[1][j-i+1])%m2 + m2)%m2);
- }
- ordered_hash(string s)
- {
- hashing(s, 0); hashing(s, 1);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment