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