Advertisement
scottish_esquire

Прямой и обратный хеш

Sep 8th, 2022
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4.  
  5. int mod = 1000000007;
  6.  
  7. int forward (std::string s, int k) {
  8.     int h = 0;
  9.     int m = 1;
  10.     for (char c : s) {
  11.         int x = (int) (c - 'a' + 1);
  12.         h = (h + m * x) % mod;
  13.         m = (m * k) % mod;
  14.     }
  15.     return h;
  16. }
  17.  
  18. int back (std::string s, int k) {
  19.     int h = 0;
  20.     for (char c : s) {
  21.         int x = (int) (c - 'a' + 1);
  22.         h = (h * k + x) % mod;
  23.     }
  24.     return h;
  25. }
  26.  
  27. int main() {
  28.     std::string s;
  29.     int k = 0;
  30.     int f = 0;
  31.     int b = 0;
  32.     std::cin >> s >> k;
  33.     f = forward(s, k);
  34.     b = back(s, k);
  35.     std::cout << f << std::endl << b;
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement