嘟嘟嘟
一看到子串排序,就想办法往后缀数组上靠。
因为后缀数组的排序和长度无关,所以我们把字符串加倍后直接一个后缀排序即可。
然后观察一下输出,其实就是(sa[i])的上一个字符,所以排完序后如果(sa[i] leqslant n)就输出(s[sa[i] + n - 1])即可。
(O(n log ^ 2 n))的,不开O2还TLE了一个点……
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 2e5 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, m, K;
char s[maxn];
int sa[maxn], rnk[maxn], tp[maxn];
bool cmp(int i, int j)
{
if(rnk[i] ^ rnk[j]) return rnk[i] < rnk[j];
int x = i + K > m ? -1 : rnk[i + K];
int y = j + K > m ? -1 : rnk[j + K];
return x < y;
}
int main()
{
scanf("%s", s + 1);
n = strlen(s + 1); m = n << 1;
for(int i = 1; i <= n; ++i) s[i + n] = s[i];
for(int i = 1; i <= m; ++i) sa[i] = i, rnk[i] = s[i];
for(K = 1; K <= m; K <<= 1)
{
sort(sa + 1, sa + m + 1, cmp);
for(int i = 1; i <= m; ++i)
tp[sa[i]] = tp[sa[i - 1]] + (cmp(sa[i- 1], sa[i]) ? 1 : 0);
for(int i = 1; i <= m; ++i) rnk[i] = tp[i];
}
for(int i = 1; i <= m; ++i) if(sa[i] <= n) putchar(s[sa[i] + n - 1]);
enter;
return 0;
}