Best Cow Line---POJ 3617(贪心)
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,目标字符串为T,那么,不断取出S的开头和末尾较小的一个
*字符放到T的末尾。上面的没有针对开头与结尾相同,如果,遇到这样动情况,应
*该比较下一个字符的大小。如果,下一个字符也相同,那么可以得到下面的算法:
*按照字典序比较S和将S反转后的字符串S’
*如果,S较小,那么取出S开头字符,追加到T的末尾
*如果,S'较小,那么取出S结尾字符,追加到T的末尾
*如果,相同则取出那一个都可以。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn =;
int n;
char s[maxn]; int main(){
cin>>n;
for( int i=; i<n; i++ ){
cin>>s[i];
}
int a=,b=n-;
int cnt=;
while(a<=b){
bool left=false;
for( int i=; a+i<=b; i++ ){
if(s[a+i]<s[b-i]){
left=true;
cnt++;
break;
}
else if(s[a+i]>s[b-i]){
left=false;
cnt++;
break;
}
}
if(left) putchar(s[a++]);
else putchar(s[b--]);
if(cnt%==) putchar('\n');
}
return ;
}
Best Cow Line---POJ 3617(贪心)的更多相关文章
- Best Cow Line (POJ 3617)
题目: 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下列任意操作. ·从S的头部删除一个字符,加到T的尾部 ·从S的尾部删除一个字符,加到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: 9284 Accepted: 2826 Des ...
- Best Cow Line(POJ No.3617)
问题: 链接:http://poj.org/problem?id=3617 思路: 按照字典序比较S和将S反转后的字符串S' 如果S较小,就从S的开头取出一个字符,加到T的末尾(更新下标值) 如果S’ ...
- Best Cow Line (POJ 3217)
给定长度为N的字符串S,要构造一个长度为N的字符串T,起初,T是一个空串,随后反复进行下列任意操作. *从S的头部删除一个字符,加到T的尾部 *从S的尾部删除一个字符,加到T的尾部 目标是要构造字典序 ...
- 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: 30684 Accepted: 8185 De ...
- 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 ...
- Best Cow Line <挑战程序设计竞赛> 习题 poj 3617
P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Goldpoj 3617 http://poj.org/problem?id=3617 题目描述FJ is about ...
随机推荐
- YOLO v3
yolo为you only look once. 是一个全卷积神经网络(FCN),它有75层卷积层,包含跳跃式传递和降采样,没有池化层,当stide=2时用做降采样. yolo的输出是一个特征映射(f ...
- Python logging日志系统
写我小小的日志系统 配置logging有以下几种方式: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用它们的配置函数: 2)创建一个日志配置文件, ...
- HTTP协议详解(三)
接着第二篇继续学习... 6 HTTP的几个重要概念 6.1连接:Connection 一个传输层的实际环流,它是建立在两个相互通讯的应用程序之间. 在http1,request和reponse头中都 ...
- oracle,mysql,sql server三大数据库的事务隔离级别查看方法
1:mysql的事务隔离级别查看方法 mysql 最简单,执行这条语句就行:select @@tx_isolation 详情: 1.查看当前会话隔离级别 select @@tx_isolation; ...
- E - Coin Change UVA - 674 &&(一些记录路径的方法)
这一道题并不难,我们只需要将dp数组先清空,再给dp[0]=1,之后就按照完全背包的模板写 主要是我们要证明着一种方法不会出现把(1+3+4)(1+4+3)当作两种方法,这一点如果自己写过背包的那个表 ...
- 高可用Redis(十):Redis原生命令搭建集群
1.搭建Redis Cluster主要步骤 1.配置开启节点 2.meet 3.指派槽 4.主从关系分配 2.环境说明 两台虚拟机,IP地址分别为:192.168.81.100和192.168.81. ...
- 机器学习实战1-K均值
本例来源于github项目:https://github.com/jakevdp/sklearn_pycon2015/blob/master/notebooks/04.2-Clustering-KMe ...
- 网络流24题——数字梯形问题 luogu 4013
题目描述:这里 极其裸的一道费用流问题 首先分析第一问,由于要求一个点只能经过一次,所以我们将梯形中的每一个点拆成两个点(记为入点和出点,顾名思义,入点用来承接上一行向这一行的边,出点用来向下一行连边 ...
- pwn学习(2)
0x00 序 之前学了蒸米大佬的ropx86,本次学习 ropx64 0x01 Leak Memory & Dynelf 蒸米大佬使用pwntools的Dynelf模块来获取函数地址, ...
- [原创]Cadence Allegro16.6安装
选择Cancel. lisence managner安装完成,然后安装Product installation Cadence OrCad Allegro SPB 16.6 完整版+和谐文件 链接:h ...