Luogu P2870 [usaco 07dec] Best ox Line, Best Cow Line, Gold, p2870usaco 07dec
Description
FJ is about to take his N (1 ≤ N ≤30,000) cows to the annual "Farmer of the Year" competition. in this contest every farmer arranges his cows in a line and herds them past the judges.
The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (I. e ., if FJ takes Bessie, Sylvia, and Dora in that order he just registers BSD ). after the registration phase ends, every group is judged in increasing lexicographic order according to the string of the initials of the cows 'names.
FJ is very busy this year and has to hurry back to his farm, so he wants to be judged as early as possible. he decides to rearrange his cows, who have already lined up, before registering them.
FJ marks a location for a new line of the competing cows. he then proceeds to convert al the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of) original line to the end of the new line. when he's finished, FJ takes his cows for registration in this new order.
Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.
Only the two sides can be retrieved at a time. The minimum Lexicographic Order is required.
Input/Output Format
Input Format:
* Line 1: A single integer: N
* Lines 2. N + 1: Line I + 1 contains a single initial ('A' .. 'Z') of the cow in the ith position in the original line
Output Format:
The least lexicographic string he can make. Every line (character t perhaps the last one) contains the initials of 80 cows ('A'... 'Z') in the new line.
Input and Output sample input sample #1: Copy
6ACDBCB
Output example #1: Copy
ABCBCD
At the beginning, I thought it was a joke. I chose the smallest one on the left and later I found out that I was a joke...
Then we start brain-filling SA,
For a string, we can find out its suffix array and prefix array for comparison.
But in this case, we need to write two SA, and the two Suffix Arrays are worse than rank.
So I went to % senior student and found that I could flip the string and copy it to the front of the string.
In this way, a SA can be used to solve the problem.
Compared with rank
Note that the question is incorrect. A line break is required for each output of 80 characters !!!! (I made another call for half an hour .)
#include<cstring>#include<iostream>#include<cstdio>using namespace std;const int MAXN=1e6+10;int N,M;int sa[MAXN],tax[MAXN],tp[MAXN],rak[MAXN],a[MAXN];void Qsort(){ for(int i=0;i<=M;i++) tax[i]=0; for(int i=1;i<=N;i++) tax[rak[i]]++; for(int i=1;i<=M;i++) tax[i]+=tax[i-1]; for(int i=N;i>=1;i--) sa[ tax[rak[tp[i]]]-- ] = tp[i];}void Ssort(){ M=450; N=N<<1|1; for(int i=1;i<=N;i++) rak[i]=a[i],tp[i]=i;Qsort(); for(int w=1,p=0;p<N;M=p,w<<=1) { p=0; for(int i=N-w+1;i<=N;i++) tp[++p]=i; for(int i=1;i<=N;i++) if(sa[i]>w) tp[++p]=sa[i]-w; Qsort(); swap(tp,rak); rak[sa[1]]=p=1; for(int i=2;i<=N;i++) rak[sa[i]]=(tp[sa[i]]==tp[sa[i-1]]&&tp[sa[i]+w]==tp[sa[i-1]+w])?p:++p; } //for(int i=1;i<=N;i++) // printf("%d ",rak[i]); int l=1,r=N/2+2,tot=0; while(tot<N/2) { tot++,rak[l]<rak[r]?printf("%c",a[l++]+'A'-1):printf("%c",a[r++]+'A'-1); if(tot%80==0) printf("\n"); } }int main(){ #ifdef WIN32 freopen("a.in","r",stdin); freopen("a.out","w",stdout); #else #endif char c; scanf("%d",&N);a[N+1]='*'; for(int i=1;i<=N;i++) { c='*';while(c<'A'||c>'Z') c=getchar(); a[N+i+1]=c-'A'+1;a[N-i+1]=c-'A'+1; } Ssort(); return 0;}