Advertisement
newb_ie

hash_wrong

Oct 15th, 2021
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3.  
  4. const long long p = 31;
  5. const long long mod = 1e9 + 7;
  6. map<int,set<int>> hs;
  7.  
  8. long long single_hash (string & s) {
  9. vector<long long> pows;
  10. pows.push_back(1);
  11. int n = (int) s.size();
  12. for (int i = 0; i < n; ++i) {
  13. pows.push_back((pows.back() * p) % mod);
  14. }
  15. vector<long long> suf_hash,pref_hash;
  16. pref_hash.push_back(((s[0] - 'a' + 1) * pows[0]) % mod);
  17. for (int i = 1; i < n; ++i) {
  18. pref_hash.push_back((pref_hash.back() + (s[i] - 'a' + 1) * pows[i]) % mod);
  19. }
  20. suf_hash.push_back(((s[n - 1] - 'a' + 1) * pows[n - 1]) % mod);
  21. for (int i = n - 2; i >= 0; --i) {
  22. suf_hash.push_back((suf_hash.back() + (s[i] - 'a' + 1) * pows[i]) % mod);
  23. }
  24. cout << pref_hash[n - 1] << ' ' << suf_hash[0] << '\n';
  25. return pref_hash[n - 1];
  26. }
  27.  
  28. int main () {
  29. ios::sync_with_stdio(false);
  30. cin.tie(nullptr);
  31. cout.tie(nullptr);
  32. string h = "aaaaa";
  33. cout << single_hash(h);
  34. //~ int T = 1;
  35. //~ cin >> T;
  36. //~ for (int test_case = 1; test_case <= T; ++test_case) {
  37. //~ int n,m;
  38. //~ cin >> n >> m;
  39. //~ for (int i = 1; i <= n; ++i) {
  40. //~ string s;
  41. //~ cin >> s;
  42. //~ cout << single_hash(s) << '\n';
  43. //~ hs[(int) s.size()].insert(single_hash(s));
  44. //~ }
  45. //~ for (int i = 1; i <= m; ++i) {
  46. //~ string s;
  47. //~ cin >> s;
  48. //~ cout << single_hash(s) << '\n';
  49. //~ }
  50. //~ }
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement