Bzoj1640: [usaco Nov] best cow line queue Transformation

Source: Internet
Author: User
1640: [usaco Nov] best cow line queue transformation time limit: 5 sec memory limit: 64 MB
Submit: 493 solved: 253
[Submit] [Status] Description

FJ plans to bring his cute N (1 ≤ n ≤ 2,000) cows to the "best old farmer Of The Year" competition. during the competition, each farmer put his milk steak into a column and then prepared to pass the test by the judges. in the competition, the initial letter of every cow is abbreviated as the first letter. For example, FJ carries Bessie, Sylvia, and Dora, this can be abbreviated as BSD. FJ only needs to rearrange a sequence of cows and then participate in the competition. he can let the first or last cow in the sequence come out and stand at the end of the new queue. to win the championship, the powerful FJ must make the Lexicographic Order of the new queue as small as possible. give you an initial sequence of cows (indicated by a letter in the first place), then form a new sequence according to the above rules, and make the Lexicographic Order of the new sequence as small as possible.

Input

Row 1st: an integer n.

Row 2nd to row n + 1: Each line has an uppercase letter, indicating the first letter of the cow in the initial sequence.

Output

The minimum lexicographic sequence. A line break is required for each output of 80 letters!

 

Sample input6
A
C
D
B
C
B
Sample outputabcbcd
Hint

 

 

Source

Silver

Problem: The same queue is changed, but it seems that the data is small and it is greedy... The code for the previous question is now... Code:
 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 7000014 #define maxm 500+10015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 using namespace std;19 inline int read()20 {21     int x=0,f=1;char ch=getchar();22     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}23     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}24     return x*f;25 }26 inline bool cmp(int *r,int a,int b,int l)27 {28     return r[a]==r[b]&&r[a+l]==r[b+l];29 }30 int wa[maxn],wb[maxn],wr[maxn],sa[maxn],rank[maxn],a[maxn],n;31 void da(int *r,int *sa,int n,int m)32 {33     int i,j,p,*x=wa,*y=wb,*t;34     for(i=0;i<m;i++)wr[i]=0;35     for(i=0;i<n;i++)wr[x[i]=r[i]]++;36     for(i=1;i<m;i++)wr[i]+=wr[i-1];37     for(i=n-1;i>=0;i--)sa[--wr[x[i]]]=i;38     for(j=1,p=1;p<n;j<<=1,m=p)39      {40         for(p=0,i=n-j;i<n;i++)y[p++]=i;41         for(i=0;i<n;i++)if(sa[i]>=j)y[p++]=sa[i]-j;42         for(i=0;i<m;i++)wr[i]=0;43         for(i=0;i<n;i++)wr[x[y[i]]]++;44         for(i=1;i<m;i++)wr[i]+=wr[i-1];45         for(i=n-1;i>=0;i--)sa[--wr[x[y[i]]]]=y[i];46         for(t=x,x=y,y=t,p=1,i=1,x[sa[0]]=0;i<n;i++)47         x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;48      }49 }50 int main()51 {52     n=read();53     for(int i=0;i<n;i++)54      {55         char ch=‘ ‘;56         while(ch<‘A‘||ch>‘Z‘)ch=getchar();57         a[i]=int(ch-‘A‘+1);58      }59     int tot=2*n+1; 60     a[n]=0;a[tot]=0;61     for(int i=1;i<=n;i++)a[n+i]=a[n-i];62     da(a,sa,tot+1,28);63     //for(int i=0;i<=tot;i++)cout<<i<<‘ ‘<<sa[i]<<endl;64     for(int i=1;i<=tot;i++)rank[sa[i]]=i;65     //for(int i=0;i<=tot;i++)cout<<i<<‘ ‘<<rank[i]<<endl;66     int l=0,r=n+1;67     while(l+r-n-1<n)68     {69         if(rank[l]<rank[r])putchar(a[l++]+‘A‘-1);else putchar(a[r++]+‘A‘-1);70         if(!((l+r-n-1)%80))putchar(‘\n‘);71     } 72     return 0;73 }
View code

 

Bzoj1640: [usaco Nov] best cow line queue Transformation

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.