# POJ 3617 Best Cow Line（最佳奶牛队伍）

POJ 3617 Best Cow Line

Time Limit: 1000MS　　Memory Limit: 65536K

 【Description】 【题目描述】 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"比赛。在这个比赛中每个农夫都会把他们的奶牛排成一队，然后经过评审团。   比赛方今年采用了一种新的登记方案：每头牛的出场都以名字首字母进行简要登记（换句话说，如果FJ带了Bessie、Sylvia和Dora参加，那么他只要登记成BSD）。登记结束后，每组评判根据奶牛名字首字母串字典序升序评判。   FJ今年事特多又得赶回农场，想早点完事。因此他决定在登记前把已经排好队的奶牛重排一遍。   FJ找了块地给比赛的奶牛排新队伍。接着他不断把第一头或最后一头牛从旧（或者剩下的）队伍转移到新队伍的尾部。搞定后，FJ会用这个新队伍登记。   给你这群奶牛的大写字母，找出上述方法排列后字典序最小的字符串。

 【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 ith position in the original line * 第1行: 一个整数: N * 第2..N+1行: 第i+1行包含表示第i个奶牛初始位置的一个大写字母('A'..'Z')

 【Output】 【输出】 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. 输出所能构造的最小字典序字符串。每行（最后一行不用管）包含80头奶牛的大写字母('A'..'Z')。

 【Sample Input - 输入样例】 【Sample Output - 输出样例】 6 A C D B C B ABCBCD

【题解】

【代码 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编辑  收藏  举报