Salvens

C

Aug 8th, 2023
981
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <array>
  4. #include <vector>
  5. #include <numeric>
  6. #include <random>
  7. #include <chrono>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. #define int long long
  13. #pragma comment(linker,"/STACK:1000000000,1000000000")
  14.  
  15. const long long INF = 1e9 + 7;
  16. const int MAXN = 2e5 + 1000;
  17. const int N = 1e5 + 10;
  18.  
  19. const int M1 = 1e9 + 123;
  20. const int M2 = 1e9 + 321;
  21. int P1 = 22811;
  22. int P2 = 22699;
  23. array<int, MAXN> power1, power2;
  24.  
  25. void init_pow() {
  26.     power1[0] = 1;
  27.     power2[0] = 1;
  28.     for (int i = 1; i < MAXN; ++i) {
  29.         power1[i] = (power1[i - 1] * P1) % M1;
  30.         power2[i] = (power2[i - 1] * P2) % M2;
  31.     }
  32. }
  33.  
  34. void build_hash(string& s, vector<pair<int, int>>& h) {
  35.     int n = s.size();
  36.     h.resize(n + 1);
  37.     h[0].first = 0;
  38.     h[0].second = 0;
  39.  
  40.     for (int i = 0; i < n; ++i) {
  41.         h[i + 1].first = (h[i].first + s[i] * power1[i]) % M1;
  42.         h[i + 1].second = (h[i].second + s[i] * power2[i]) % M2;
  43.     }
  44. }
  45.  
  46. pair<int, int> get_hash(vector<pair<int, int>>& h, int pos, int len, int mx_pow = 0) {
  47.     int h1 = h[pos + len].first - h[pos].first;
  48.     int h2 = h[pos + len].second - h[pos].second;
  49.     if (h1 < 0) {
  50.         h1 += M1;
  51.     }
  52.     if (h2 < 0) {
  53.         h2 += M2;
  54.     }
  55.     if (mx_pow != 0) {
  56.         h1 = h1 * power1[mx_pow - (pos + len - 1)] % M1;
  57.         h2 = h2 * power2[mx_pow - (pos + len - 1)] % M2;
  58.     }
  59.     return make_pair(h1, h2);
  60. }
  61.  
  62. bool is_equal(vector<pair<int, int>>& h_l, vector<pair<int, int>>& h_r, int start1, int start2, int len) {
  63.     pair<int, int> d_l, d_r;
  64.     d_l.first = ((h_l[start1 + len].first - h_l[start1].first + M1) % M1 * power1[start2]) % M1;
  65.     d_l.second = ((h_l[start1 + len].second - h_l[start1].second + M2) % M2 * power2[start2]) % M2;
  66.  
  67.     d_r.first = ((h_r[start2 + len].first - h_r[start2].first + M1) % M1 * power1[start1]) % M1;
  68.     d_r.second = ((h_r[start2 + len].second - h_r[start2].second + M2) % M2 * power2[start1]) % M2;
  69.  
  70.     return d_l == d_r;
  71. }
  72.  
  73. int random_key(const int before, const int after) {
  74.     auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
  75.     std::mt19937 mt_rand(seed);
  76.     int base = std::uniform_int_distribution<int>(before + 1, after)(mt_rand);
  77.     return base;
  78. }
  79.  
  80. signed main() {
  81.     ios_base::sync_with_stdio(false);
  82.     cin.tie(nullptr);
  83.     cout.tie(nullptr);
  84.  
  85.     P1 = random_key(256, M1);
  86.     P2 = random_key(256, M2);
  87. //    cout << P1 << ' ' << P2 << endl;
  88.  
  89.     string a;
  90.     cin >> a;
  91.     int n = a.size();
  92.     a = a + a;
  93.  
  94.     vector<pair<int, int>> h_a;
  95.     init_pow();
  96.     build_hash(a, h_a);
  97.  
  98.     vector<int> pos;
  99.     for (int i = 0; i < n; ++i) {
  100.         pos.emplace_back(i);
  101.     }
  102.  
  103.     int ans = *min_element(pos.begin(), pos.end(), [&] (const int p1, const int p2) {
  104.         int l = 0, r = n + 1;
  105.         while (r - l > 1) {
  106.             int mid = (r + l) / 2;
  107.             if (get_hash(h_a, p1, mid, 2 * n) == get_hash(h_a, p2, mid, 2 * n)) {
  108.                 l = mid;
  109.             } else {
  110.                 r = mid;
  111.             }
  112.         }
  113.         return l < n && a[p1 + l] < a[p2 + l];
  114.     });
  115.  
  116.     cout << a.substr(ans, n) << '\n';
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment