1 #include<cstdio>
2 #include<cstdlib>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<iostream>
7 #include<vector>
8 #include<map>
9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 250000+10000
14 #define maxm 500+100
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 using namespace std;
19 string s,ss;
20 int n,a[maxn],sa[maxn],wa[maxn],wb[maxn],wr[maxn],wv[maxn];
21 inline bool cmp(int *r,int a,int b,int l)
22 {
23 return r[a]==r[b]&&r[a+l]==r[b+l];
24 }
25 void da(int *r,int *sa,int n,int m)
26 {
27 int i,j,p,*x=wa,*y=wb,*t;
28 for(i=0;i<m;i++)wr[i]=0;
29 for(i=0;i<n;i++)wr[x[i]=r[i]]++;
30 for(i=1;i<m;i++)wr[i]+=wr[i-1];
31 for(i=n-1;i>=0;i--)sa[--wr[x[i]]]=i;
32 for(j=1,p=1;p<n;j<<=1,m=p)
33 {
34 for(p=0,i=n-j;i<n;i++)y[p++]=i;
35 for(i=0;i<n;i++)if(sa[i]>=j)y[p++]=sa[i]-j;
36 for(i=0;i<m;i++)wr[i]=0;
37 for(i=0;i<n;i++)wr[x[y[i]]]++;
38 for(i=1;i<m;i++)wr[i]+=wr[i-1];
39 for(i=n-1;i>=0;i--)sa[--wr[x[y[i]]]]=y[i];
40 for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
41 x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
42 }
43 }
44 int main()
45 {
46 freopen("input.txt","r",stdin);
47 freopen("output.txt","w",stdout);
48 cin>>s;n=s.length();
49 for(int i=0;i<n;i++)a[i]=int(s[i]);
50 for(int i=0;i<n;i++)a[n+i]=int(s[i]);
51 a[2*n]=0;
52 da(a,sa,2*n+1,129);
53 ss="";
54 for(int i=0;i<=2*n;i++)
55 if(sa[i]<n)ss=ss+s[(sa[i]-1+n)%n];
56 cout<<ss<<endl;
57 return 0;
58 }