poj 3617 Best Cow Line
http://poj.org/problem;jsessionid=F0726AFA441F19BA381A2C946BA81F07?id=3617
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.
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 这个题说的是一个人有一群牛,要去参加比赛,每头牛都有名字,报名的时候只登记名字的首字母,但是这个人比赛当天有急事要回去,所以就想先比赛,而比赛是按字典序来安排的,所以它的字典序越小就越靠
前,这个人要组成一个比较小的字符串,他可以把原来的字符串的头部删除放在新的字符串的末尾,也可以把原来字符串的尾部删除放在新的的尾部,大致就是这个意思
然后书上说只要前面的比较小就行,然后就是不断地取开头和末尾放在新的字符串的尾部,
书上还说,字典序比较S和反转后的字符串S‘,如果S比较小就从S的开头取一个字符,如果S’的比较小,就取S‘的
但是!!!!我理解错了啊!!!我理解成一个字符串反转,然后对于每一个进行比较,较小的拿出来
就是 ACDBCB
反转后 BCBDCA
然后A跟B比,A比较小,就取A,C跟C比,取C,D跟B比取B,然后就得到了ACBBCA,哎,我还一直纠结为什么答案不对
#include<cstdio>
int main()
{
int n;
char s[2000],c[2000];
char t[2000];
scanf("%d",&n);
getchar();//输入%d(也就是数字)后面肯定有回车或者空格你要打,那么缓冲区就有\n或者空格字符遗留,%c 读取是从缓冲区读取的,那么优先读取的是\n,就错了,getchar()读掉遗留的就行了
for(int i=0;i<n;i++)
{
scanf("%c",&s[i]);
}
for(int i=n-1;i>=0;i--)
{
c[n-i-1]=s[i];//这个我测试的时候一直输出少一个,原来是因为getchar()
}
for(int i=0;i<n;i++)
{
if(s[i]<=c[i])
t[i]=s[i];
if(s[i]>c[i])
t[i]=c[i];
}
for(int i=0;i<n;i++)
printf("%c",t[i]);
printf("\n");
return 0;
}
虽然写了一大堆,但是不对,不过没关系,至少是自己写的啊
#include<cstdio>
int main()
{
int n;
char s[2000];
scanf("%d",&n);
getchar();//输入%d(也就是数字)后面肯定有回车或者空格你要打,那么缓冲区就有\n或者空格字符遗留,%c 读取是从缓冲区读取的,那么优先读取的是\n,就错了,getchar()读掉遗留的就行了
for(int i=0;i<n;i++)
{
scanf("%c",&s[i]);
}
int a=0,b=n-1;
while(a<=b)
{
bool left=false;
for(int i=0;a+i<=b;i++)
{
if(s[i+a]<s[b-i])
{
left=true;
break;
}
else if(s[a+i]>s[b-i])
{
left=false;
break;
}
}
if(left)
putchar(s[a++]);
else
putchar(s[b--]);
}
putchar('\n');
return 0;
}这个按书上打的计较不行,presentation error
这个我感觉主要是Every line (except perhaps the last one) contains the initials of 80 cows ('A'..'Z') in the new line的问题
然后我就上网查了别人的代码,但是现在有事,明天再补,我习惯想到一点补一点,以前没人看,我怎样写都无所谓,但是现在有人看了,还是谨慎一点吧
果然就是那个问题
#include<cstdio>
int main()
{
int n,num;
char s[2000];
while(scanf("%d",&n)==1)
{
num=0;
for(int i=0;i<n;i++)
{ getchar();
scanf("%c",&s[i]);
}
int a=0,b=n-1;
while(a<=b)
{
bool left=false;
for(int i=0;a+i<=b;i++)//这个是为了防止一样的情况
{
if(s[i+a]<s[b-i])
{
left=true;
break;
}
else if(s[a+i]>s[b-i])
{
left=false;
break;
}
}
if(left)
putchar(s[a++]);//a和b的值在不断变化
else
putchar(s[b--]);
num++;
if(num%80==0)
puts(" ");
}
putchar('\n');
}
return 0;
}这样就对了
poj 3617 Best Cow Line的更多相关文章
- POJ 3617 Best Cow Line(最佳奶牛队伍)
POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...
- POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心
带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...
- POJ 3617 Best Cow Line (贪心)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted: 4 ...
- poj 3617 Best Cow Line (字符串反转贪心算法)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9284 Accepted: 2826 Des ...
- POJ 3617 Best Cow Line 贪心算法
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26670 Accepted: 7226 De ...
- poj 3617 Best Cow Line 贪心模拟
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42701 Accepted: 10911 D ...
- POJ 3617 Best Cow Line (模拟)
题目链接 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Yea ...
- poj 3617 Best Cow Line 解题报告
题目链接:http://poj.org/problem?id=3617 题目意思:给出一条长度为n的字符串S,目标是要构造一条字典序尽量小,长度为n的字符串T.构造的规则是,如果S的头部的字母 < ...
- POJ 3617 Best Cow Line (字典序最小问题 & 贪心)
原题链接:http://poj.org/problem?id=3617 问题梗概:给定长度为 的字符串 , 要构造一个长度为 的字符串 .起初, 是一个空串,随后反复进行下列任意操作. 从 的头部删除 ...
随机推荐
- android几个实用的判定代码
之前项目有几个判定代码很实用,特此做一个整理. 一.验证手机格式是否正确 //判断手机号码是否合理 private boolean judgePhoneNums(String phoneNums) { ...
- TestStand与LabVIEW UI 交互
交互起因 客户觉得TestStand界面复杂,希望一个简单的界面即可进行序列执行,采用LabVIEW调用TestStand引擎可实现快速设计,将TestStand拆解到LabVIEW.然而,这样做需要 ...
- BizTalk开发系列(八) BizTalk Server 常识整理
1.什么是BizTalk Server? BizTalk 是业务流程管理服务器,用于连接人员,流程,有效管理和提升业务所需的信息.在原有版本业务 流程管理和SOA/ESB 的基础上,第5 个版 ...
- centos安装后iptables基本设置
一.首先关闭防火墙#service iptables stop 二.查看状态,确认关闭#service iptables status 三.清除掉防火墙规则#iptables -F#iptables ...
- SQL ORDER BY 子句
ORDER BY 语句用于对结果集进行排序. ORDER BY 语句 ORDER BY 语句用于根据指定的列对结果集进行排序. ORDER BY 语句默认按照升序对记录进行排序. 如果您希望按照降序对 ...
- Python之调用函数
Python之调用函数 Python内置了很多有用的函数,我们可以直接调用. 要调用一个函数,需要知道函数的名称和参数,比如求绝对值的函数 abs,它接收一个参数. 可以直接从Python的官方网站查 ...
- Linux内核设计第八周 ——进程的切换和系统的一般执行过程
Linux内核设计第八周 ——进程的切换和系统的一般执行过程 第一部分 知识点总结 第二部分 实验部分 1.配置实验环境,确保menu内核可以正常启动 2.进入gdb调试,在shedule和conte ...
- swift2.0单例
import Foundation public class SwiftSingleton{ var name = "000" public static le ...
- 加载UI
weak情况 1 2 3 4 @property (weak,nonatomic) UILabel *nameLabel; UILabel *nameLabel = [[UILabel alloc ...
- Mysql乱码
MySql字符集 1.系统默认的.数据库默认的.表格默认的.列的 真正决定权在列定义上 2.latin1 系统默认字符编码 字符范围是0x00-0xff,可以存放任意编码的字符序列. 3.utf8编码 ...