1692. [Usaco07dec] Queue transform "suffix array + greedy"

Source: Internet
Author: User

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 Output

Abcbcd

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

Greedy + suffix Array
Positive str+0+ anti-STR constructs a string to seek SA
Pointer i,j refers to the leftmost two strings (equivalent to starting from the beginning and from the tail)
Two pointer positions are small, if equal, the suffix dictionary sequence is small
Greedy proof is not very good, on the internet for half a day and only one is also good:

Obviously if F[L]>G[R], then if S[l + 0..p] = = S[R-0..P] Equal,
Next S[l + p + 1] > s[r-p-1] must be unequal, and s[r-p-1] better,
If it's exactly equal, then it means the same.

#include <iostream> #include <cstring> #include <cstdio> #define MAXN (70000+10) using namespace std; int wa[maxn],wb[maxn],wt[maxn];int Height[maxn],sa[maxn],rank[maxn];char R[maxn];int n,m=130,k;    BOOL CMP (int *y,int a,int b,int k) {int arank1=y[a];    int brank1=y[b];    int arank2=a+k>=n?-1:y[a+k];    int brank2=b+k>=n?-1:y[b+k]; return Arank1==brank1 && arank2==brank2;} void Build_sa () {int *x=wa,*y=wb;for (int i=0;i<m;++i) wt[i]=0;for (int i=0;i<n;++i) wt[x[i]=r[i]]++;for (int i=1;i <m;++i) wt[i]+=wt[i-1];for (int i=n-1;i>=0;--i) sa[--wt[x[i]]]=i;for (int j=1;j<=n;j<<=1) {int p=0;for ( int i=n-j;i<n;++i) y[p++]=i;for (int i=0;i<n;++i) if (sa[i]>=j) y[p++]=sa[i]-j;for (int i=0;i<m;++i) wt[i]=0 ; for (int i=0;i<n;++i) wt[x[y[i]]]++;for (int i=0;i<m;++i) wt[i]+=wt[i-1];for (int i=n-1;i>=0;--i) SA[--wt[x[y [i]]] =y[i];m=1;swap (x, y); x[sa[0]]=0;for (int i=1;i<n;++i) x[sa[i]]=cmp (y,sa[i],sa[i-1],j)? M-1:m++;if (m>=n) retUrn;} }int Main () {scanf ("%d", &n), scanf ("%s", R), r[n]= ' 0 '; n=n*2+1;for (int i= (n-1)/2+1;i<n;++i) r[i]=r[n-i-1]; Build_sa (); for (int i=0;i<n;++i) Rank[sa[i]]=i;int l=0,r= (n-1)/2+1;int sum=0;while (sum< (n-1)/2) {if (R[l]==r[r] ) rank[l]<rank[r]?printf ("%c", r[l++]):p rintf ("%c", r[r++]); elser[l]<r[r]?printf ("%c", r[l++]):p rintf ("%c", r[r++]); ++sum;if (sum%80==0) printf ("\ n");}}

1692. [Usaco07dec] 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.