1692: [Usaco2007 Dec] Queue transform time limit:5 Sec Memory limit:64 MB
submit:1383 solved:582
[Submit] [Status] [Discuss] Description
FJ intends to take his n (1 <= n <= 30,000) cows to the annual "All-American farmer's Grand Prix". In this game, each contestant has to make his cows in a row, and then lead them through the referee's table in turn. This year, the competition Committee adopted a new registration rule when it accepted the team: they took out the first letters of the cows ' names in all the teams and lined them up in the order they corresponded to the cows in the queue (for example, if FJ the cows were Bessie, Sylvia, Dora, The registrant registered the team as BSD). After the registration, the organizing committee arranges the registration names of all the teams in ascending order of the dictionaries and obtains their appearances. FJ recently had a lot of things, so he wasn't going to waste too much time on the game, which means he wanted to play as early as possible. So he intends to readjust the pre-designed team for the cows. The FJ Adjustment method is this: each time, he in the original queue at the beginning or end of a cow, put her to the end of the new queue, and then the remaining cow queue repeat the above operation, until all cows are inserted into the new queue. The resulting queue is the final cow queue that fj to register. The next thing is for you: for a given cow's initial position, it calculates the smallest possible sequence of dictionaries that can be obtained according to FJ's adjustment rules.
Input
* Line 1th: An integer: N
* 2nd. N+1: Line i+1 only has 1 ' a '. The letter in Z ', indicating the first letter of a cow's name in the queue from the head to the end of the first
Output
* 1th ...?? Line: The queue with the smallest dictionary order that the output FJ can get. Each line (except the last line) outputs exactly 80 ' a '. The letter in Z ', which indicates the first letter of each cow name in the new queue
Sample Input6
A
C
D
B
C
B
Input Description:
FJ has 6 cows in line: ACDBCB
Sample OutputAbcbcd
Output Description:
Operand Original Queue new queue
#1 ACDBCB
#2 CDBCB A
#3 Cdbc AB
#4 CDB ABC
#5 CD ABCB
#6 D ABCBC
#7 ABCBCD
HINT Source
Gold
How to say, suffix array helper greedy
Greed is obvious, dictionary order as far as possible to choose the small
The question is, how do you choose the characters on both ends?
Take a look at the ends of one after the left to the right and from right to left who has a smaller dictionary order .... The same words do not matter, or the small side of the election will be more excellent
How do I quickly compare the dictionary order of prefixes and suffixes?
The entire string is flipped with a special character and then at the end, the suffix array asks for RNK
Tips:
Both ends can head=1 tail=n1+2, that is, the right end using the left end of the flip, convenient point
////main.cpp//bzoj1692////Created by Candy on 2016/12/29.//copyright©2016 year Candy. All rights reserved.//#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intn=6e4+5;intn,n1,n2,m;Chars[3],a[n];intSa[n],c[n],t1[n],t2[n];inlineBOOLcmpint*r,intAintBintj) { returna+j<=n&&b+j<=n&&r[a]==r[b]&&r[a+j]==r[b+j];}intRnk[n],height[n];voidGetHeight (Chars[]) { intk=0; for(intI=1; i<=n;i++) rnk[sa[i]]=i; for(intI=1; i<=n;i++){ if(k) k--; if(rnk[i]==1)Continue; intj=sa[rnk[i]-1]; while(I+k<=n&&j+k<=n&&s[i+k]==s[j+k]) k++; Height[rnk[i]]=K; }}voidGetsa (Chars[]) { int*r=t1,*k=T2; for(intI=0; i<=m;i++) c[i]=0; for(intI=1; i<=n;i++) c[r[i]=s[i]]++; for(intI=1; i<=m;i++) c[i]+=c[i-1]; for(inti=n;i>=1; i--) sa[c[r[i]]--]=i; for(intj=1; j<=n;j<<=1){ intp=0; for(inti=n-j+1; i<=n;i++) k[++p]=i; for(intI=1; i<=n;i++)if(SA[I]>J) k[++p]=sa[i]-J; for(intI=0; i<=m;i++) c[i]=0; for(intI=1; i<=n;i++) c[r[k[i]]]++; for(intI=1; i<=m;i++) c[i]+=c[i-1]; for(inti=n;i>=1; i--) sa[c[r[k[i]]]--]=K[i]; Swap (r,k);p=0; r[sa[1]]=++p; for(intI=2; i<=n;i++) r[sa[i]]=cmp (k,sa[i],sa[i-1],J)? p:++p; if(p>=n) Break; m=p; }}intCnt=0, Head,tail;voidsolve () {head=1; tail=n1+2; for(intD=1;d <=n1;d++){ if(Rnk[head]<rnk[tail]) Putchar (a[head++]); ElsePutchar (a[tail++]); if(d% the==0) Putchar ('\ n'); }}intMain () {scanf ("%d",&N); for(intI=1; i<=n;i++) scanf ("%s", s), a[i]=s[0]; A[n+1]=1; for(intI=1; i<=n;i++) a[n+1+i]=a[n+1-i]; N1=N; N=n+n+1; M= -; Getsa (a); GetHeight (a); Solve ();}
Bzoj 1692: [Usaco2007 Dec] queue transform [suffix array greedy]