Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int mod = 1e9 + 7;
- long long bin_pow(int a, int n, int m = mod)
- {
- if(n == 0)
- return 1;
- if(n == 1)
- return a;
- long long t = bin_pow(a, n / 2, m);
- return (((t * t) % m) * (n % 2 ? a : 1)) % m;
- }
- int main()
- {
- // Ускорение ввода
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- // Ввод/вывод из файла
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- // Ввод
- string s;
- cin >> s;
- int n = s.size();
- //hash
- long long hash[n + 1], rhash = 0;
- hash[0] = 0;
- for(int i = 1; i <= n; i++)
- {
- hash[i] = (hash[i - 1] + ((s[i - 1] - 'a' + 1) * bin_pow(29, i)) % mod) % mod;
- rhash = (rhash + ((s[i - 1] - 'a' + 1) * bin_pow(29, n - i + 1)) % mod) % mod;
- }
- if(rhash == hash[n])
- {
- cout << "";
- return 0;
- }
- string ans;
- long long suf_hash = 0;
- for(int i = 1; i <= n; i++)
- {
- rhash = (rhash * 29) % mod;
- suf_hash = (suf_hash * 29) % mod;
- suf_hash = (suf_hash + ((s[i - 1] - 'a' + 1) * bin_pow(29, n + 1)) % mod) % mod;
- ans.push_back(s[i - 1]);
- if((rhash + hash[i]) % mod == (hash[n] + suf_hash) % mod)
- {
- reverse(ans.begin(), ans.end());
- cout << ans;
- exit(0);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement