istinishat

Own_Hashing

Dec 3rd, 2018
615
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define MOD 1000000007
  5. #define MOD1 1000000009
  6. #define PR 43
  7. #define PR1 37
  8. #define ll long long
  9. #define MAX 200005
  10.  
  11. int bigmod(ll a,ll p,int md)
  12. {
  13.     long long ret = 1;
  14.     while(p) {
  15.         if(p & 1) ret = ret * a % md;
  16.         a = a * a % md;
  17.         p >>= 1;
  18.     }
  19.     return ret;
  20. }
  21.  
  22. int mod_mul(ll a,ll b,int md){return (a*b)%md;}
  23. int mod_plus(ll a,ll b,int md){return (a+b)%md;}
  24. int mod_sub(ll a,ll b,int md){a-=b;if(a<0)a+=md;return a;}
  25.  
  26. int all_hash[MAX][2],inv[MAX][2];
  27.  
  28.  
  29.  
  30. void calc_hash(string str)
  31. {
  32.     int xx,xx1,pw,pw1;
  33.     xx=xx1=0;
  34.     pw=pw1=1;
  35.     for(int i=0;i<str.size();i++){
  36.         xx=mod_plus(xx,mod_mul(pw,str[i],MOD),MOD);
  37.         xx1=mod_plus(xx1,mod_mul(pw1,str[i],MOD1),MOD1);
  38.  
  39.         all_hash[i][0]=xx;
  40.         all_hash[i][1]=xx1;
  41.  
  42.         inv[i][0]=bigmod(pw,MOD-2,MOD);
  43.         inv[i][1]=bigmod(pw1,MOD1-2,MOD1);
  44.  
  45.         pw=mod_mul(pw,PR,MOD);
  46.         pw1=mod_mul(pw1,PR1,MOD1);
  47.     }
  48. }
  49.  
  50. pair<int,int> substring_hash(int l,int r)
  51. {
  52.     if(l==0)return make_pair(all_hash[r][0],all_hash[r][1]);
  53.  
  54.     int xx= mod_sub(all_hash[r][0],all_hash[l-1][0],MOD);
  55.     int xx1=mod_sub(all_hash[r][1],all_hash[l-1][1],MOD1);
  56.  
  57.     xx=mod_mul(xx,inv[l][0],MOD);
  58.     xx1=mod_mul(xx1,inv[l][1],MOD1);
  59.  
  60.     return make_pair(xx,xx1);
  61. }
  62.  
  63. int main()
  64. {
  65.     //freopen("input.txt","r",stdin);
  66.     int i,j,k,t,a;
  67.  
  68.     string str;
  69.     cin>>str;
  70.     calc_hash(str);
  71.  
  72.     while(1){
  73.  
  74.         int l,r;
  75.         cin>>l>>r;
  76.         pair<int,int>p=substring_hash(l,r);
  77.         cout<<"---->  "<<p.first<<' '<<p.second<<endl;
  78.     }
  79.  
  80.  
  81.  
  82.     return 0;
  83. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×