Best Cow Line
64-bit integer IO format: %lld Java class name: Main
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
眼残 忽视了这个
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个为一行输出
ac代码
using namespacestd;
char str0[2002];
char str1[2002];
intmain()
{
int n;
while (cin>>n)
{
for (int i =0; i < n; i++) cin >> str0[i];int a =0;
int b = n - 1;
int k = 0;
while (a<=b)
{
bool flag = false;
for (int i =0; a + i <= b; i++)
{
if (str0[a + i] < str0[b - i])
{
flag = true;
break;
}
else if (str0[a + i] > str0[b - i])
{
flag = false;
break;
}
}
if (flag) putchar(str0[a++]);
else putchar(str0[b--]);
k++;
if (k == 80)
{
puts("");
k = 0;
}
}
puts("");
}
return 0;
}