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 <stdio.h>
using namespace std;
int n;
char s[2100];int main()
{scanf("%d",&n);int cnt=0;char temp[2];while(cnt<n){scanf("%s",temp);s[cnt++]=temp[0];}int f=0,e=n-1,cnt0=0;while(f <= e)   // 将从左起和从右起的字符串比较{bool flag = false;for(int i = 0; i <= e - f; i++)  // 字符串的开头与结尾相同,则继续比较下一个{if(s[f+i] < s[e-i]){flag = true;cnt0++;break;}else if(s[f+i] > s[e-i]){flag = false;cnt0++;break;}}if(flag) cout<<s[f++];else cout<<s[e--];if(cnt0 % 80 == 0)cout<<endl;}cout  << endl;//cout << "Hello world!" << endl;return 0;
}

Best Cow Line相关推荐

  1. 贪心  POJ - 3617 ​​​​​​​Best Cow Line

    Best Cow Line POJ - 3617 FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of ...

  2. P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold(加强版)(贪心+hash哈希)

    P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold(加强版)(贪心+hash哈希) 洛谷上这道水题丧心病狂地把数据范围加到了500000 普通的做法肯定A不了了, ...

  3. 【BZOJ】3301: [USACO2011 Feb] Cow Line(康托展开)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3301 其实这一题很早就a过了,但是那时候看题解写完也是似懂非懂的.... 听zyf神犇说是康托展开, ...

  4. POJ 3617 Best Cow Line 贪心算法

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

  5. POJ 3617 Best Cow Line(最佳奶牛队伍)

    POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...

  6. P2870 [USACO07DEC]Best Cow Line G

    P2870 [USACO07DEC]Best Cow Line G 题意: 给你一个字符串,每次从首或尾取一个字符组成字符串,问所有能够组成的字符串中字典序最小的一个. 题解: 现在要组成字典序最小的 ...

  7. Cow Line(洛谷-P3014)

    题目描述 The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of thei ...

  8. 【BZOJ】【3301】【USACO2011 Feb】Cow Line

    康托展开 裸的康托展开&逆康托展开 康托展开就是一种特殊的hash,且是可逆的-- 康托展开计算的是有多少种排列的字典序比这个小,所以编号应该+1:逆运算同理(-1). 序列->序号:( ...

  9. 3403: [Usaco2009 Open]Cow Line 直线上的牛

    3403: [Usaco2009 Open]Cow Line 直线上的牛 Time Limit: 3 Sec  Memory Limit: 128 MB Submit: 71  Solved: 62 ...

  10. POJ3617 Best Cow Line【水题】

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

最新文章

  1. 科普篇:贝叶斯网络中的置信度传播
  2. 如何快速定位程序Core?
  3. Linux网络IO精华指南
  4. 单例模式创建的两种方法
  5. Beetlex之tcp/tls服务压测工具
  6. C#EF中,使用类似于SQL中的% 模糊查询
  7. Eclipse连接hdfs
  8. iOS开发之时区的获取,设置,创建
  9. 一线实践 | 借助混沌工程工具 ChaosBlade 构建高可用的分布式系统
  10. 怎么计算机械需要的电机,步进电机力矩的怎么计算?
  11. 百度 bos php,百度BOS云存储上传插件 - 支持客户端直传、服务端中转、分片上传 – 基于ThinkPHP和Bootstrap的极速后台开发框架...
  12. Java项目:问卷调查系统(java+SSM+layui+JSP+Mysql)
  13. 什么牌子的洗地机最适合家用?洗地机品牌排行榜前十名推荐
  14. Java开发必备:java帮助文档网址下载或在线帮助
  15. Zebra POS打印机Wifi无线打印方案和Android实现
  16. Android R 11 后台定位权限没有 始终允许选项的解决方法
  17. 路径之谜 java_路径之谜 - 给杰瑞一块奶酪~ - 博客园
  18. Linux串口测试工具
  19. 【Qt+FFmpeg】鼠标滚轮放大、缩小、移动——解码播放本地视频(三)
  20. 分页存储的地址空间是一维的,分段存储的地址空间是二维的

热门文章

  1. 什么叫做石英表_石英表和机械表的区别是什么
  2. 炼油机出来的什么油_办一个废轮胎炼油设备厂有哪些注意事项
  3. php setcookie使用变量,PHP setcookie() 函数
  4. 织梦html编辑器功能在哪,织梦dede自带编辑器替换百度ueditor编辑器
  5. apache 编译支持php_apache2 不支持php文件 解决办法(示例代码)
  6. OpenCV人脸检测与人脸识别
  7. 三、Git多人开发:不同人修改了同文件的不同区域如何处理
  8. TensorBoard(二)
  9. 用kali的Meterpreter控制win10,可以偷看你的电脑的所有数据
  10. 如何配置mysql8.0_安装和配置Mysql8.0