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(贪心)的更多相关文章

  1. Best Cow Line (POJ 3617)

    题目: 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下列任意操作. ·从S的头部删除一个字符,加到T的尾部 ·从S的尾部删除一个字符,加到T的尾部 目标是要构 ...

  2. POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心

    带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...

  3. poj 3617 Best Cow Line (字符串反转贪心算法)

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9284   Accepted: 2826 Des ...

  4. Best Cow Line(POJ No.3617)

    问题: 链接:http://poj.org/problem?id=3617 思路: 按照字典序比较S和将S反转后的字符串S' 如果S较小,就从S的开头取出一个字符,加到T的末尾(更新下标值) 如果S’ ...

  5. Best Cow Line (POJ 3217)

    给定长度为N的字符串S,要构造一个长度为N的字符串T,起初,T是一个空串,随后反复进行下列任意操作. *从S的头部删除一个字符,加到T的尾部 *从S的尾部删除一个字符,加到T的尾部 目标是要构造字典序 ...

  6. POJ 3617 Best Cow Line (贪心)

    Best Cow Line   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted: 4 ...

  7. POJ 3617:Best Cow Line(贪心,字典序)

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30684   Accepted: 8185 De ...

  8. POJ 3617 Best Cow Line 贪心算法

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26670   Accepted: 7226 De ...

  9. poj 3617 Best Cow Line 贪心模拟

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42701   Accepted: 10911 D ...

  10. Best Cow Line <挑战程序设计竞赛> 习题 poj 3617

    P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Goldpoj 3617 http://poj.org/problem?id=3617 题目描述FJ is about ...

随机推荐

  1. Spring Cloud 之 服务注册与发现实战

    一. 启动Eureka Server集群 准备二台云主机,二个eureka server服务互相进行复制.准备二个application.yml配置,分别如下: application-server1 ...

  2. linux中gcc和g++的区别

    1.两者都是编译器 2.gcc编译c语言:g++既可以编译c语言,也可以编译c++语言 3.gcc不能自动链接库文件,一般用g++来链接库文件,非要用gcc的话,一般使用gcc -lstdc++命令 ...

  3. struts 学习03

    jdk下载: 使用注解: @ParenPackage(value="struts-default") @Namespace(value="/) @Action(value ...

  4. Flask简介&入门

    1 了解框架 Flask作为Web框架,它的作用主要是为了开发Web应用程序.那么我们首先来了解下Web应用程序.Web应用程序 (World Wide Web)诞生最初的目的,是为了利用互联网交流工 ...

  5. js &运算符什么意思,什么用处

    “&&”连接两个表达式,当两侧表达式都为真时,返回TRUE.有一个为假则返回FALSE. 也就是说,符号前面的如果为true,就会执行符号后面的语句,如果符号前面的为false,那么后 ...

  6. 2018-2019-2 网络对抗技术 20165314 Exp4 恶意代码分析

    一.原理与实践说明 1.实践目标 监控你自己系统的运行状态,看有没有可疑的程序在运行. 分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sysinternals,s ...

  7. 第一阶段——CentOS6_Python3.6.1笔记(尚学堂-Python基础快速入门)+ 【补充】麦子-Python程序入门与进阶

    虚拟机环境: 设置网络 .修改网络地址 .设置网卡为nat模式 .确保物理机启动dhcp.net服务 .编辑文件:vim /etc/sysconfig/network-scripts/ifcfg-et ...

  8. Spring 源码学习系列

    前言 Spring框架之于 JavaEE 程序员来说,犹如锄头之于农民.Java 程序员每天都要使用Spring框架,Spring框架也确实是个可手的工具. 最初使用Spring的时候,我们需要配置m ...

  9. PHP实现url参数组合字符串与数组相互转换

    $data = array( 'name' => 'tom', 'sex' => 1, 'channel' => 'ty' ); 数组转url参数字符串 $queryStr = ht ...

  10. mac抓包工具anyproxy

    本文以 mac为代理,ios手机为客户端举例. 文档地址:http://anyproxy.io/ 1.环境配置: 安装  node  :参考   https://www.jianshu.com/p/3 ...