Flip the string back to the back of the original string, with a separator in the middle, each time greedy selection Rank The little one.
It's actually a template for practicing a suffix array.
#include <algorithm>#include <iostream>#include <cstdlib>#include <cstring>#include <vector>#include <cstdio>#include <queue>#include <cmath>#include <set>#include <map>#define INF 0x7FFFFFFF#define LL Long Long#define N 1000005using namespace STD;CharGet () {CharC=getchar (); for(;c>' Z '|| c<' A '; C=getchar ());returnC;}CharS[n];intT1[n],t2[n],cc[n],sa[n],rank[n];intN,m,len;BOOLcmpint*y,intAintBintK) {intA1=Y[A],B1=Y[B];inta2=a+k>=len?-1: Y[a+k];intb2=b+k>=len?-1: Y[b+k];returnA1==B1&&A2==B2;}voidMake_sa () {int*x=t1,*y=t2,m=2666; for(intI=0; i<len;i++) cc[x[i]=s[i]]++; for(intI=1; i<m;i++) cc[i]+=cc[i-1]; for(inti=len-1; ~i;i--) sa[--cc[x[i]]]=i; for(intk=1; k<len;k<<=1) {intp=0; for(inti=len-k;i<len;i++) y[p++]=i; for(intI=0; i<len;i++)if(sa[i]>=k) Y[p++]=sa[i]-k; for(intI=0; i<m;i++) cc[i]=0; for(intI=0; i<len;i++) ++cc[x[y[i]]; for(intI=1; i<m;i++) cc[i]+=cc[i-1]; for(inti=len-1; ~i;i--) Sa[--cc[x[y[i]]]]=y[i]; Swap (x, y); x[sa[0]]=0; m=1; for(intI=1; i<len;i++) x[sa[i]]=cmp (y,sa[i],sa[i-1],k)? m-1: m++;if(M>len) Break; } for(intI=0; i<len;i++) rank[sa[i]]=i;}intMain () {scanf("%d", &n); s[n]=' $ '; len=2*n+1; for(intI=0; i<n;i++) s[2*n-i]=s[i]=get (); Make_sa ();intL=0, r=n-1; for(intI=1; i<=n;i++) {if(rank[l]<rank[2*N-R])Putchar(s[l++]);Else Putchar(s[r--]);if(i% the==0)puts(""); }return 0;}
1692: [Usaco2007 Dec] Queue transform | suffix Array | greedy