fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdio>
  4. #include <cstring>
  5. #define BASE 7
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. long long pow[10], a = 1;
  11. for (int i = 1; i < 10; ++i)
  12. {
  13. a *= BASE;
  14. pow[i] = a;
  15. }
  16. pow[0] = 1;
  17. long long hash[10];
  18. memset (hash, 0, sizeof(hash));
  19. string s = "abcdab";
  20. for (int i = s.size()-1; i >= 0; --i)
  21. {
  22. if (i < s.size()-1)
  23. hash[i] = hash[i+1] * BASE;
  24. hash[i] += (s[i] - 'a') + 1;
  25. }
  26. printf ("Hash of 'abc' is %d\n", hash[0] - (hash[3] * pow[strlen("abc")]));
  27. return 0;
  28. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
Hash of 'abc' is 162