Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- using LL = long long;
- #define fastio ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
- const int BASE = 1e9 + 7;
- const int P = 131;
- const int MAXN = 1e5;
- vector<int> power(2*MAXN + 1, 0);
- void init_power()
- {
- power[0] = 1;
- for(size_t i=1; i<power.size(); ++i)
- power[i] = (1LL * power[i-1] * P) % BASE;
- }
- struct HashString
- {
- string val;
- size_t size;
- vector<int> h; // hash-value on prefix
- HashString( string pval ): val(pval), size(val.size()), h(size + 1, 0)
- {
- for(size_t i=1; i<=size; ++i)
- h[i] = (h[i-1] + 1LL * val[i-1] * power[i-1]) % BASE;
- }
- };
- void solve()
- {
- init_power();
- string s;
- cin >> s;
- size_t n = s.size();
- HashString ss(s+s);
- int pmn = 0;
- for(size_t k=1; k<n; ++k)
- {
- int pcur = k;
- // binary search equal prefix len
- int left = 0, right = n + 1;
- while (right - left > 1)
- {
- int mid = (left + right) / 2;
- // pmn + mid, pcur + mid - indexes of substring's begin
- int h1 = (ss.h[pmn+mid] - ss.h[pmn] + BASE) % BASE;
- int h2 = (ss.h[pcur+mid] - ss.h[pcur] + BASE) % BASE;
- if ( (1LL * h1 * power[pcur-pmn]) % BASE == h2 )
- left = mid;
- else
- right = mid;
- }
- if ( left < n )
- {
- if (ss.val[pcur + left] < ss.val[pmn + left])
- pmn = pcur;
- }
- }
- cout << ss.val.substr(pmn, n);
- }
- int main()
- {
- fastio
- #ifdef AZHUKOV
- freopen("input.txt", "r", stdin);
- #endif // AZHUKOV
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement