1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<cstdlib>
5 using namespace std;
6
7 #define maxn 200010
8 char s[maxn];
9 int sa[maxn],t[maxn];
10 int t2[maxn],c[maxn],n;
11
12 inline void build(int m)
13 {
14 int i,*x = t,*y = t2;
15 memset(c,0,4*(m+1));
16 for (i = 0;i < n;++i) c[x[i] = s[i]]++;
17 for (i = 1;i < m;++i) c[i] += c[i-1];
18 for (i = n-1;i >= 0;--i) sa[--c[x[i]]] = i;
19 for (int k = 1;k < n;k <<= 1)
20 {
21 int p = 0;
22 for (i = n - k;i < n;++i) y[p++] = i;
23 for (i = 0;i < n;++i) if (sa[i] >= k) y[p++] = sa[i]-k;
24 memset(c,0,4*(m+1));
25 for (i = 0;i < n;++i) c[x[y[i]]]++;
26 for (i = 1;i < m;++i) c[i] += c[i-1];
27 for (i = n - 1;i >= 0;--i) sa[--c[x[y[i]]]] = y[i];
28 swap(x,y);
29 p = 1; x[sa[0]] = 0;
30 for (i = 1;i < n;++i)
31 x[sa[i]] = (y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k] ? p - 1:p++);
32 if (p >= n) break;
33 m = p;
34 }
35 }
36
37 int main()
38 {
39 freopen("1031.in","r",stdin);
40 freopen("1031.out","w",stdout);
41 scanf("%s",s);
42 n = strlen(s);
43 for (int i = n;i < 2*n-1;++i) s[i] = s[i - n];
44 n <<= 1;
45 build(128);
46 for (int i = 0;i < n;++i) if (sa[i] < (n >> 1)) printf("%c",s[sa[i]+(n >> 1) - 1]);
47 fclose(stdin); fclose(stdout);
48 return 0;
49 }