Best Cow Line
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 13950 |
|
Accepted: 3987 |
Description
FJ is an about-to-take he N (1≤ n ≤2,000) cows to the annual ' Farmer of the Year ' competition. In this contest every farmer arranges he 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 would appear (i.e., If FJ takes Bessie, Sylvia, and Dora in that order he just registers BSD). After the registration phase ends, every group was judged in increasing lexicographic order according to the string of the Initials of the cows ' names.
FJ is very busy this year and have to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange him cows, who has already lined up, before registering them.
FJ marks a location for a new line of the competing cows. He then proceeds to marshal the cows from the old line to the new one by repeatedly sending either the first or last cow I n the (remainder of the) original line to the end of the new line. When he's finished, FJ takes his cows for registration on this new order.
Given the initial order of he cows, determine the least lexicographic string of initials he can make this.
Input
* Line 1: A single integer: N
* Lines 2. N+1:line i+1 contains a single initial (' A ' ... ' Z ') of the cow in the i-th position in the original line
Output
The least lexicographic string he can make. Every line (except perhaps, the last one) contains the initials of the cows (' A ' ... ' Z ') in the new line.
Sample Input
6ACDBCB
Sample Output
Abcbcd
Source
Usaco November Silver simple greed. Compare the characters on both ends, equal to the next, the small end of the first added to the answer sequence, repeated n times. The greedy proof is not difficult to think of.
#include <cstdio>#include<cstring>#include<string>#include<iostream>using namespacestd;intN;stringans;Charline[2005];intMain () {scanf ("%d", &N); for(inti =0; I < n; ++i) {Chars[2]; scanf ("%s", s); Line[i]= s[0]; } ans=""; intL =0, R = N-1; while(n--) { inti = l, j =R; while(I < J && line[i] = = Line[j]) ++i,--J; if(Line[i] <Line[j]) {ans+=Line[l]; ++l; } Else{ans+=Line[r]; --R; }} printf ("%c", ans[0]); for(inti =1; I < ans.length (); ++i) {ifI the==0) printf ("\ n"); printf ("%c", Ans[i]); } return 0;}
"POJ 3617" Best Cow Line