POJ 3617 Best Cow Line
来源:互联网 发布:nodejs 异步 数据库 编辑:程序博客网 时间:2024/06/05 00:38
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
6ACDBCB
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;
}
- POJ-3617-Best Cow Line
- POJ 3617 Best Cow Line
- POJ:3617 Best Cow Line
- POJ 3617 Best Cow Line
- POJ-3617 Best Cow Line
- poj 3617 Best Cow Line
- poj 3617 Best Cow Line
- POJ 3617 Best Cow Line
- POJ 3617 Best Cow Line
- POJ 3617 Best Cow line
- POJ Best Cow Line 3617
- POJ 3617 Best Cow Line
- Best Cow Line(POJ-3617)
- POJ 3617 Best Cow Line
- POJ 3617 Best Cow Line
- POJ-3617 Best Cow Line
- poj 3617 Best Cow Line
- POJ 3617 Best Cow Line
- 文件打开模式
- swift压缩图片的方法
- Docke镜像和仓库基础命令
- javascript实现九九乘法表
- Ubuntu成长记录(一)
- POJ 3617 Best Cow Line
- module
- 隐藏NavigationBar下边线
- Java常见内存溢出(OOM)解决方案
- rm -rf或者rmdir失败的特殊情况
- js 精确的乘法结果
- Docx4j获取文档中的图片
- 元类会自动将你通常传给‘type’的参数作为自己的参数传入
- gcc 编译顺序问题