1 #include <bits/stdc++.h>
2 using namespace std;
3 char s[200005];
4 int sa[200005], wv[2][350005], tong[200005];
5
6 bool cmp(int *tmp, int x, int y, int j)
7 {
8 return tmp[x] == tmp[y] && tmp[x + j] == tmp[y + j];
9 }
10
11 void getsa(int n, int m)
12 {
13 int p = 0, *x = wv[0], *y = wv[1];
14 for(int i = 0; i < n; ++i)
15 ++tong[x[i] = s[i]];
16 for(int i = 1; i < m; ++i)
17 tong[i] += tong[i - 1];
18 for(int i = n - 1; ~i; --i)
19 sa[--tong[x[i]]] = i;
20 for(int j = 1; p != n; j <<= 1, m = p)
21 {
22 p = 0;
23 for(int i = n - j; i < n; ++i)
24 y[p++] = i;
25 for(int i = 0; i < n; ++i)
26 if(sa[i] >= j) y[p++] = sa[i] - j;
27 for(int i = 0; i < m; ++i)
28 tong[i] = 0;
29 for(int i = 0; i < n; ++i)
30 ++tong[x[y[i]]];
31 for(int i = 1; i < m; ++i)
32 tong[i] += tong[i - 1];
33 for(int i = n - 1; ~i; --i)
34 sa[--tong[x[y[i]]]] = y[i];
35 swap(x, y), p = 1, x[sa[0]] = 0;
36 for(int i = 1; i < n; ++i)
37 x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
38 }
39 }
40
41 int main()
42 {
43 int n;
44 cin >> s;
45 n = strlen(s);
46 for(int i = 0; i < n; ++i)
47 s[i + n] = s[i];
48 getsa(n << 1 | 1, 128);
49 for(int i = 1; i <= n << 1; ++i)
50 if(sa[i] < n) cout << s[sa[i] + n - 1];
51 cout << endl;
52 return 0;
53 }