https://www.luogu.org/problem/show?pid=2870
#include<bits/stdc++.h>
#define rank fuck
#define Ll long long
using namespace std;
const int N=6e4+5;
struct zz{int id,x,y;}a[N];
int rank[N],sa[N],h[N];
int n,m,x,y,z;
char c[N];
bool cmp(zz a,zz b){if(a.x!=b.x)return a.x<b.x;return a.y<b.y;}
void make(){
for(int i=1;i<=n;i++)rank[i]=c[i];
for(int i=0;(1<<i)<=n;i++){
for(int j=1;j<=n;j++){
a[j].id=j;
a[j].x=rank[j];
a[j].y=(j+(1<<i))<=n?rank[j+(1<<i)]:0;
}
sort(a+1,a+n+1,cmp);
int num=0;
for(int j=1;j<=n;j++){
num+=(a[j].x!=a[j-1].x||a[j].y!=a[j-1].y);
rank[a[j].id]=num;
}
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)cin>>c[i];
c[n+1]=' ';
for(int i=1;i<=n;i++)c[n+1+i]=c[n-i+1];
m=n;
n=n*2+1;
make();
int l=1,r=m;
while(m--){
if(c[l]>c[r])printf("%c",c[r--]);else
if(c[l]<c[r])printf("%c",c[l++]);else{
int x=rank[l];
int y=rank[n-r+1];
if(x>y)printf("%c",c[r--]);else printf("%c",c[l++]);
}
if(++z%80==0)printf("\n");
}
}