CamolaZ

AAlgorithms1Lodz

Mar 4th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. unsigned const int W = 997;
  6. unsigned const int S = 128; //ascci value that we are used in our test!
  7.  
  8. unsigned int hash(char *s, int len){
  9.     unsigned h=0;
  10.     for(int k=0; k<len;k++)
  11.     h=(h*S + s[k])%W;
  12. return h;
  13. }
  14.  
  15. unsigned int rehash(unsigned int spower, char oldc, char inewc, unsigned int oldhash){
  16.     return (((oldhash - oldc * spower)* S + inewc) % W+W)%W;
  17. }
  18.  
  19. int main()
  20. {
  21.  
  22.     cout << hash("Cona",4) << endl;
  23.     cout << rehash(3,'n','m',5) << endl;
  24.     return 0;
  25. }
Add Comment
Please, Sign In to add comment