SHARE
TWEET

Untitled

a guest Oct 16th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <vector>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <set>
  5. #include <algorithm>
  6. #include <map>
  7. #include <list>
  8. #include <string>
  9. #include <ctime>
  10. #include <random>
  11. #include <stack>
  12. #include <iomanip>
  13. #include <deque>
  14. #include <queue>
  15. #include <ctime>
  16. #include <bitset>
  17. #include <fstream>
  18.  
  19. using namespace std;
  20. typedef long long ll;
  21. const long long inf = 2e9;
  22. const long long mod = 1e9 + 7;
  23.  
  24. string s, r;
  25. int n;
  26. vector<ll>pr;
  27. vector<ll>po;
  28. ll b = 31;
  29.  
  30. long long get_hash(int pos) {
  31.     return (pr[pos + n] - (pr[pos] * po[n]) % mod + mod) % mod;
  32. }
  33.  
  34. int main() {
  35.     ios_base::sync_with_stdio(0);
  36.     cin.tie(0);
  37.     cout.tie(0);
  38.     cin >> n;
  39.     cin >> s;
  40.     r = s;
  41.     reverse(r.begin(), r.end());
  42.     s += s;
  43.     pr.resize(2 * n + 1);
  44.     po.resize(2 * n + 1);
  45.     po[0] = 1;
  46.     for (int i = 1; i <= 2 * n; i++) {
  47.         po[i] = po[i - 1] * b;
  48.         po[i] %= mod;
  49.     }
  50.     for (int i = 1; i <= n * 2; i++) {
  51.         pr[i] = (pr[i - 1] * b + (s[i - 1] - 'a' + 1)) % mod;
  52.     }
  53.     ll ch = 0;
  54.     ll t = 1;
  55.     for (int i = 1; i <= n; i++) {
  56.         ch += (po[n-i] * (r[i - 1] - 'a' + 1)) % mod;
  57.         ch %= mod;
  58.     }
  59.     for (int i = 0; i < n; i++) {
  60.         if (get_hash(i) == ch) {
  61.             cout << i;
  62.             //system("pause>nul");
  63.             return 0;
  64.         }
  65.     }
  66.     cout << -1;
  67.     //system("pause>nul");
  68.     return 0;
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top