Test instructions: Enter a string to minimize it through a ring transform. (n<=300000)
The bare suffix sort ah!
(the card often puts his life off)
Suffix array Remember to open the big.
1#include <bits/stdc++.h>2 using namespacestd;3 4 intx[1200005],y[1200005],u[1200005],v[1200005],r[1200005],o[1200005],n,m=700000;5 intstr[600005],buf[600005];6 7 intMain () {8scanf"%d",&n);9 for(intI=1; i<=n;i++) scanf ("%d", &str[i]), buf[i]=Str[i];TenSort (buf+1, buf+n+1); One for(intI=1; i<=n;i++) Str[i]=lower_bound (buf+1, buf+n+1, Str[i])-buf; A for(intI=1; i<=n;i++) str[i+n]=Str[i]; - -n*=2; the - for(RegisterintI=1; i<=n;i++) u[str[i]]++; - for(RegisterintI=1; i<=m;i++) u[i]+=u[i-1]; - for(Registerinti=n;i>=1; i--) x[u[str[i]]--]=i; +r[x[1]]=1; - for(intI=2; i<=n;i++) r[x[i]]=r[x[i-1]]+ (str[x[i]]!=str[x[i-1]]); + A for(intL=1; r[x[n]]<n;l<<=1) { atmemset (U,0,sizeofu); -memset (V,0,sizeofv); -memcpy (O,r,sizeofR); - for(RegisterintI=1; i<=n;i++) u[r[i]]++, v[r[i+l]]++; - for(RegisterintI=1; i<=n;i++) u[i]+=u[i-1], v[i]+=v[i-1]; - for(Registerinti=n;i>=1; i--) y[v[r[i+l]]--]=i; in for(Registerinti=n;i>=1; i--) x[u[r[y[i]]]--]=Y[i]; -r[x[1]]=1; to for(RegisterintI=2; i<=n;i++) +r[x[i]]=r[x[i-1]]+ ((o[x[i]]!=o[x[i-1]])|| (O[x[i]+l]!= (o[x[i-1]+l] )); - } the * for(intI=1; i<=n;i++) $ if(x[i]<=n/2) {Panax Notoginseng for(Registerintj=1; j<=n/2; j + +) printf ("%d", buf[str[x[i]+j-1]]); - return 0; the } +}
Valley 1368 craft-suffix array