"BZOJ-1692" queue transform suffix array + greedy

Source: Internet
Author: User

1692: [Usaco2007 Dec] Queue transform time limit:5 Sec Memory limit:64 MB
submit:1153 solved:482
[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
Hintsource

Gold

Solution

Suffix array

Can be taken out before and after, so actually need to prefix, then build crossdress good, even behind the original string

Then think of a greedy strategy:

The title requires the smallest dictionary order, that is, the first team to take a smaller tail to get out, then if there is a similar abbcdbba situation, take the first team or the end of the team is more embarrassing

Only the prefix suffix can be compared. Then you can compare with rank, very good to write

Code
#include <cstdio>#include<cstring>#include<cmath>using namespacestd;#defineMAXN 60010CharS[MAXN];intSa[maxn],len;intWA[MAXN],WB[MAXN],WS[MAXN],WV[MAXN];intcmpint*r,intAintBintl) {    returnr[a]==r[b]&&r[a+l]==r[b+l];}voidCAOChar(RNint*sa,intNintm) {    intp,*x=wa,*y=wb,*T;  for(intI=0; i<m; i++) ws[i]=0;  for(intI=0; i<n; i++) ws[x[i]=r[i]]++;  for(intI=1; i<m; i++) ws[i]+=ws[i-1];  for(inti=n-1; i>=0; i--) sa[--ws[x[i]]]=i; P=1; for(intj=1; p<n; j*=2, m=p) {p=0; for(intI=n-j; i<n; i++) y[p++]=i;  for(intI=0; i<n; i++)if(SA[I]&GT;=J) y[p++]=sa[i]-J;  for(intI=0; i<n; i++) wv[i]=X[y[i]];  for(intI=0; i<m; i++) ws[i]=0;  for(intI=0; i<n; i++) ws[wv[i]]++;  for(intI=1; i<m; i++) ws[i]+=ws[i-1];  for(inti=n-1; i>=0; i--) sa[--ws[wv[i]]]=Y[i]; T=x,x=y,y=t;p=1; x[sa[0]]=0;  for(intI=1; i<n; i++) X[sa[i]]=CMP (y,sa[i-1],sa[i],j)? p1:p + +; }}intRANK[MAXN],HEIGHT[MAXN];voidCalheight (Char(RNint*sa,intN) {    intk=0;  for(intI=1; i<=n; i++) rank[sa[i]]=i;  for(intI=0; i<n; height[rank[i++]]=k) {k? k--:0; for(intj=sa[rank[i]-1]; R[I+K]==R[J+K]; k++);}}intMain () {intN; scanf ("%d\n", &n);Chars[2];  for(intI=1; i<=n; i++) scanf ("%s", s), s[len++]=s[0]; S[len++]='@';  for(inti=n-1; i>=0; i--) s[len++]=S[i]; S[len]=0; DA (S,sa,len+1, $);    Calheight (S,sa,len); intL=0, r=n+1, cnt=0;  while(cnt<N) {cnt++; if(Rank[l]<rank[r]) Putchar (S[l]), l++; ElsePutchar (S[r]), r++; if(cnt% the==0) puts (""); }    return 0;}

Easy 1A Series. The data is a bit weak. At the beginning of the hand is missing a statement even 1 a ...

"BZOJ-1692" queue transform suffix array + greedy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.