[POJ]3617 Best Cow Line 贪心
https://cn.vjudge.net/problem/POJ-3617 题目大意 输入一些字符 然后你可...

12
2019/08

# [POJ]3617 Best Cow Line 贪心

Best Cow Line

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.

## 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

## 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.

### Sample Input

6
A
C
D
B
C
B

### Sample Output

ABCBCD

FJ即将带着他的N头(1≤N≤2000)奶牛参加一年一度的“年度农民”大赛。在这场比赛中，每个农民都把他的奶牛排成一排，并把它们赶过评委。

FJ今年很忙，必须赶回他的农场，所以他想尽早被评判。他决定重新安排他的奶牛，它们已经排好队了，然后再登记。
FJ标志着一个新的竞争奶牛生产线的位置。然后，他通过不断地将原行的第一头或最后一头奶牛发送到新行末尾，将奶牛从旧行引导到新行。当他完成后，FJ把他的牛登记在这个新订单。

### 代码

package POJ;

import java.util.Scanner;

public class _3617_BestCowLine {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
StringBuilder s = new StringBuilder();
for (int i = 0; i < n; i++) {
s.append(cin.next());
}
f(s, n);
}

private static void f(StringBuilder s, int n) {
String s1 = s.toString();
String s2 = new StringBuilder(s).reverse().toString();

int cnt = 0;
StringBuilder rec = new StringBuilder("");
while (rec.length() < n) {
if (s1.compareTo(s2) <=0) {
rec.append(s1.charAt(0));
s1=s1.substring(1);
} else {
rec.append(s2.charAt(0));
s2=s2.substring(1);
}
if(rec.length()%80==0){
System.out.println(rec.substring(cnt*80,++cnt*80));
}
}
if(rec.length()>cnt*80){
System.out.println(rec.substring(cnt*80));
}
}
}

百度已收录

Last modification：August 13th, 2019 at 01:03 am