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. Acitivity(活动)

    1.首先活动是放在返回栈中. 2.活动的生命周期

  2. 【easy】141. Linked List Cycle

    非常简单的题:判断链表有没有环(用快慢指针) /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...

  3. qml layout

    一.使用总结 1.锚点:锚点锚在父控件左边anchors.left: parent.left ,才可以设置anchors.leftMargin:20 才有作用,设置anchors.topMargin: ...

  4. 圈水池 nyoj 78 凸包算法

    圈水池 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 有一个牧场,牧场上有很多个供水装置,现在牧场的主人想要用篱笆把这些供水装置圈起来,以防止不是自己的牲畜来喝水, ...

  5. linux 笔记整理

    1.常使用的进程管理命令 ps   显示显示主动的进程     .  top 显示所用运行的进程   .  关闭进程   kill [pro Number ]  .  关闭名为H的所有进程 killa ...

  6. DISPLAY变量和xhost(原创)

    DISPLAY 在Linux/Unix类操作系统上, DISPLAY用来设置将图形显示到何处. 直接登陆图形界面或者登陆命令行界面后使用startx启动图形, DISPLAY环境变量将自动设置为:0: ...

  7. Linux基础 -Ubuntu

    Ubuntu 下: sudo 以管理员权限执行 apt 是Advanced Packaging Tool ,Ubuntu下的安装包管理工具,早期使用apt-get,从Ubuntu16开始建议使用apt ...

  8. Knockout 官网学习文档目录

    官网:https://knockoutjs.com/documentation/introduction.html Knockout-Validation: https://github.com/Kn ...

  9. spring boot mybatis打印SQL语句

    在logback-spring.xml 文件中添加 <logger name="com.ibatis" level="DEBUG" /> <l ...

  10. thinkPHP5扩展workerman

    -安装workerman 首先通过 composer 安装 composer require topthink/think-worker -vvv 如果报错: Installation failed, ...