POJ 3617 Best Cow Line(最佳奶牛队伍)

POJ 3617 Best Cow Line

Time Limit: 1000MS  Memory Limit: 65536K



FJ is about to take his N (1 ≤ N ≤ 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 marshal the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of the) 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.

FJ打算带他的N(1 ≤ N ≤ 2,000)头奶牛去参加"Farmer of the Year"比赛。在这个比赛中每个农夫都会把他们的奶牛排成一队,然后经过评审团。












* Line 1: A single integer: N

* Lines 2..N+1: Line i+1 contains a single initial ('A'..'Z') of the cow in the ith position in the original line

* 第1行: 一个整数: N

* 第2..N+1行: 第i+1行包含表示第i个奶牛初始位置的一个大写字母('A'..'Z')




The least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows ('A'..'Z') in the new line.



【Sample Input - 输入样例】

【Sample Output - 输出样例】













【代码 C++】

 1 #include<cstdio>
 2 char data[2005];
 3 int i;
 4 bool cmp(int L, int R){
 5     if (data[L] == data[R]){
 6         while (data[L] == data[R] && L < R) ++L, --R;
 7     }
 8     return data[L] < data[R];
 9 }
10 void opt(char now){
11     if (i == 80) i = 0, puts("");
12     putchar(now), ++i;
13 }
14 int main(){
15     int L, R, n;
16     scanf("%d", &n);
17     for (i = 0; i < n; ++i) getchar(), data[i] = getchar();
18     for (i = L = 0, R = n - 1; L <= R;){
19         if (cmp(L, R)) opt(data[L++]);
20         else opt(data[R--]);
21     }
22     return 0;
23 }


posted @ 2016-03-23 00:46  Simon_X  阅读(1036)  评论(0编辑  收藏  举报