Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- int mod = 1000000007;
- int forward (std::string s, int k) {
- int h = 0;
- int m = 1;
- for (char c : s) {
- int x = (int) (c - 'a' + 1);
- h = (h + m * x) % mod;
- m = (m * k) % mod;
- }
- return h;
- }
- int back (std::string s, int k) {
- int h = 0;
- for (char c : s) {
- int x = (int) (c - 'a' + 1);
- h = (h * k + x) % mod;
- }
- return h;
- }
- int main() {
- std::string s;
- int k = 0;
- int f = 0;
- int b = 0;
- std::cin >> s >> k;
- f = forward(s, k);
- b = back(s, k);
- std::cout << f << std::endl << b;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement