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. centos7 搭建jenkins服务器

    具体可参考:https://blog.csdn.net/it_lihongmin/article/details/80814384 注意: 1. 修改主目录:默认是/var/lib/jenkins,  ...

  2. 一、学习起步vue——安装

    学习vue第一步:安装 (windows系统) 整个运行的命令:  npm -v node -v 查看版本 npm uninstall -g vue-cli 卸载vue-cli npm install ...

  3. 从头开始学gradle【Gradle 构建基础】

    构建基础 Project 和 task:projects 和 tasks是 Gradle 中最重要的两个概念. 任何一个 Gradle 构建都是由一个或多个 projects 组成.每个 projec ...

  4. C# 常用类型校验Validate

    using System.Text; using System.Text.RegularExpressions; namespace 落地页测试代码 { public class Validate { ...

  5. MongoDB数据库(一):基本操作

    1.NoSQL的概念 "NoSQL"一词最早于1998年被用于一个轻量级的关系数据库的名字 随着web2.0的快速发展,NoSQL概念在2009年被提了出来 NoSQL最常见的解释 ...

  6. docker环境下elasticsearch安装ik和拼音分词

    elasticsearch拼音分词地址:https://github.com/medcl/elasticsearch-analysis-pinyin/releases 在elasticsearch下面 ...

  7. github配置ssh密钥的方法

    配置用户名和邮箱 初次安装git需要配置用户名和邮箱,否则git会提示:please tell me who you are. 你需要运行命令来配置你的用户名和邮箱: $ git config --g ...

  8. centos7 firewalld 开放端口

    开通80端口 firewall-cmd --zone=public --add-port=80/tcp --permanent --zone #作用域 --add-port=80/tcp #添加端口, ...

  9. java servlet的域对象

    在进行网络编程中的项目时 经常用到的域对象主要包括以下三种: 1. ServletContext  作用范围比较大 代码如下: //一个请求代码: ServletContext sc = reques ...

  10. 异步简析之BlockingCollection实现生产消费模式

    目前市面上有诸多的产品实现队列功能,比如Redis.MemCache等... 其实c#中也有一个基础的集合类专门用来实现生产/消费模式 (生产模式还是建议使用Redis等产品) 下面是官方的一些资料和 ...