1 #include<iostream>
2 #include<cstring>
3 using namespace std;
4
5 const int MAXN=300000;
6 int WA[MAXN],WB[MAXN],WV[MAXN],WS[MAXN];
7 int Rank[MAXN],Height[MAXN];
8
9 int cmp(int *r , int a, int b, int l)
10 {
11 return r[a] == r[b] && r[a+l] == r[b+l];
12 }
13 void DA(char *r , int *sa , int n, int m)
14 {
15 int i, j, p, *x = WA, *y = WB;
16 for(i = 0; i < m; i++) WS[i] = 0;
17 for(i = 0; i < n; i++) WS[x[i] = r[i]]++;
18 for(i = 1; i < m; i++) WS[i] += WS[i-1];
19 for(i = n-1; i >= 0; i--) sa[--WS[x[i]]] = i;
20
21 for(j = 1,p = 1; p < n ; j <<= 1,m = p)
22 {
23 for(p = 0, i = n - j; i < n; i++) y[p++]=i;
24 for(i = 0; i < n; i++)
25 if(sa[i] >= j)
26 y[p++] = sa[i] - j;
27 for(i = 0; i < n; i++) WV[i] = x[y[i]];
28 for(i = 0; i < m; i++) WS[i] = 0;
29 for(i = 0; i < n; i++) WS[WV[i]]++;
30 for(i = 1; i < m; i++) WS[i] += WS[i-1];
31 for(i = n-1; i >= 0; i--) sa[--WS[WV[i]]] = y[i];
32 for(swap(x,y),p = 1,x[sa[0]] = 0,i = 1; i < n;i++)
33 x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
34 }
35 }
36
37 void calheight(char *r,int *sa,int n)
38 {
39 int i,j,k=0;
40 for(i=1;i<=n;i++)Rank[sa[i]]=i;
41 for(i=0;i<n;Height[Rank[i++]]=k)
42 for(k?k--:0,j=sa[Rank[i]-1];r[i+k]==r[j+k];k++);
43 return;
44 }
45
46 char str[MAXN];
47 int sa[MAXN];
48
49 int main()
50 {
51 cin>>(str+1);
52 int n=strlen(str+1);
53 for(int i=1;i<=n;i++) str[i+n]=str[i];
54 str[0]=0;
55 DA(str,sa,2*n+1,256);
56 //calheight(str,sa,n);
57 for(int i=1;i<=2*n;i++)
58 if(sa[i]<=n)
59 cout<<str[sa[i]+n-1];
60 return 0;
61 }