Best cow line
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:9284 |
|
Accepted:2826 |
Description
FJ is about to take hisN(1 ≤NLess than 2,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.
Input
* Line 1: A single INTEGER:N
* Lines 2 ..N+ 1: LineI+ 1 contains a single initial ('A' .. 'Z') of the cow inITh position in the original line
Output
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.
Sample Input
6ACDBCB
Sample output
ABCBCD
AC code:
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<string> 6 #include<cmath> 7 #include<map> 8 #include<queue> 9 using namespace std;10 char s[2010],s1[2010];11 int compare(int x,int y,int t)12 {13 //cout<<x<<"**"<<y<<endl;14 //cout<<"*"<<s[1]<<"*"<<endl;15 // cout<<s[x]<<"^^"<<s1[y]<<endl;16 int j=0;17 while(j<=t)18 {19 20 if(s[x]<s1[y])21 return 1;22 else if(s[x]>s1[y])23 return 2;24 x++;y++;j++;25 }26 return 0;27 }28 int main()29 {30 int i,j,k,t,m,n;31 while(cin>>t)32 {33 for(int i=0,j=t-1;i<t;i++,j--)34 {35 cin>>s[i];36 s1[j]=s[i];37 }38 n=t;39 int x=0,y=0,ans=0;40 while(n--)41 {42 int kk=compare(x,y,n);43 if(kk==1 || kk==0)44 {45 cout<<s[x];46 x=x+1;47 }48 else if(kk==2)49 {50 cout<<s1[y];51 y=y+1;52 }53 ans++;54 if(ans%80==0)55 cout<<endl;56 }57 }58 return 0;59 }