Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***
- created: 2022-06-19-23.41.40
- ***/
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
- #define ll long long
- #define ull unsigned long long
- #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
- #define get_lost_idiot return 0
- #define nl '\n'
- ull p[200005];
- ull hash1[200005];
- ull rhash1[200005];
- ull base=307;
- void power()
- {
- p[0]=1;
- for(int i=1;i<=200003;i++)
- {
- p[i]=p[i-1]*base;
- }
- }
- void hashing(string a)
- {
- hash1[0]=0;
- for(int i=1; i<=a.size(); i++)
- {
- hash1[i]=(hash1[i-1]*base+a[i-1]);
- }
- rhash1[a.size()+1]=0;
- for(int i=a.size(); i>=1;i--)
- {
- rhash1[i]=(rhash1[i+1]*base + a[i-1]);
- }
- }
- ull forwardhash(ll l,ll r)
- {
- return (hash1[r] - (hash1[l-1] * p[r-l+1]));
- }
- ull backwardhash(ll l,ll r)
- {
- return (rhash1[l] - (rhash1[r+1] * p[r-l+1]));
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- power();
- test
- {
- ll n,m,i,j,k,l,h;
- string a;
- cin>>a;
- n=a.size();
- a+=a;
- hashing(a);
- k=1;
- for(i=2;i<=n;i++)
- {
- l=0,h=n-1;
- while(l<=h)
- {
- ll mid=(l+h)/2;
- if(forwardhash(k,k+mid)==forwardhash(i,i+mid))
- {
- l=mid+1;
- }
- else
- {
- h=mid-1;
- }
- }
- if(l<=n-1)
- {
- if(a[i+l-1]<a[k+l-1])
- {
- k=i;
- }
- }
- }
- cout<<a.substr(k-1,n)<<nl;
- }
- get_lost_idiot;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement