Best Cow Line
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30454   Accepted: 8126

Description

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

先简单翻译一下题目:

给定长度为N的字符串S,构造长度为N的新字符串T。T最初是一个空的字符串,每次从S的头部或尾部删除一个字符,添加到T的尾部,使T的字典序尽可能的小。图示如下:

图片截取自《挑战程序设计竞赛》第二版(秋叶拓哉)

这是一个经典的贪心算法题,每次在S的头尾中选择较小的添加到T尾,如果S的头尾相同,则比较头和尾的下一个字符,以此类推,于是便可得到如下算法:

将S反转,比较S与反转后的S的字典序,从S的头尾中选择小的加入到T的末尾,如果头尾相同,则可取任意一个。

#include<iostream>
#include<string>
using namespace std;const int MAX = 2010;char cow[MAX];void solve(int n)
{string str = "";int left = 0, right = n - 1;bool flag = false;while(left <= right)   //比较头尾大小 {for(int i = 0; i <= right - left; i++) if(cow[left + i] < cow[right - i]){flag = true;break;}else if(cow[left + i] > cow[right - i]){flag = false;break;}if(flag)str = str + cow[left++];elsestr = str + cow[right--];}for(int i = 0; i < str.length(); i++) {if(i % 80 == 0 && i) //每80个输出一个换行符 cout << endl;cout << str[i];}cout << endl;
}int main()
{int n;cin >> n;for(int i = 0; i < n; i++)cin >> cow[i];solve(n);return 0;
}

POJ 3618 Best Cow Line(贪心算法)相关推荐

  1. POJ 3617 Best Cow Line 贪心算法

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

  2. POJ 3617 Best Cow Line 贪心

    不能单纯比较头尾两个字符,应该比较头尾两个字符串. //#pragma comment(linker, "/STACK:1024000000,1024000000") #inclu ...

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

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

  4. 贪心  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 ...

  5. bzoj4278[ONTAK2015]Tasowanie bzoj1692[USACO 2007Dec]队列变换(Best Cow Line) 贪心正确性证明...

    做法网上到处都有就不说了. 这题其实是之前做的-.不过由于人太傻现在才想明白比较字典序进行贪心的正确性-. 方便起见,在两个串的最右端都加上很大但不相同的字符,避免第lcp+1个字符不存在的边界. 如 ...

  6. POJ 3617 Best Cow Line

    给定长度N的字符串S,要求构造一个长度为N的字符串T,起初T是一个空串,随后反复进行如下操作: 1.从字符串S的首部删除一个字符添加到T的末尾. 2.从字符串S的尾部删除一个字符添加到T的末尾. 要求 ...

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

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

  8. 贪心算法—区间调度 电影节(POJ 4151)

    贪心算法--区间选取问题 或是区间调度问题 本文解决一个很经典的贪心算法问题 Interval Scheduling(区间调度问题).给你很多形如[start,end]的闭区间,请你设计一个算法,算出 ...

  9. 贪心算法—建立雷达(POJ 1328)

    贪心算法--区间选点问题 这也是贪心算法的经典问题,一般情况为:有n个闭区间[ai,bj],取尽量少的点,使得每个区间内都至少有一个点. 分析 如果区间i内已经有一个点被取到,则称此区间已经被满足. ...

最新文章

  1. 规格选择_Axure教程:实现商品规格选择功能
  2. Swift学习------常量与变量
  3. 分享一下字符串匹配BM算法学习心得。
  4. python2018上海脱产_python接口自动化27-urlencode编码%E6%82%A0%E6%82%A0与解码
  5. Oracle数据库迁移-基础
  6. javascript精要(1)-script元素
  7. 对java面向对象的三大特征的理解_Java面向对象的三大特征是什么?
  8. 超形象!流体版的勾股定理演示动图...
  9. 人生苦短,Python之父要解开这个困惑
  10. java反序列化异常接不到_由Java对象反序列化异常想到的
  11. CXF WebService 开发文档-目录索引
  12. 华三 h3c Vlan静态路由
  13. 聊天室 作业 java_java大作业设计报告-java聊天室.docx
  14. 扇贝计算机英语单词,扇贝英语单词
  15. 【机器人学】正运动学详解
  16. 三菱Q系列总线型项目程序全套,三菱PLC程序
  17. Button控件更改背景和去掉边框
  18. 剁手节致敬!听当年的老人讲述阿帕网(互联网前身)诞生的故事
  19. Power BI——柱形图
  20. SYD88811新DTM测试

热门文章

  1. 使用拼音搜索中文(转载)
  2. 14. Linux系统服务管理
  3. c#五子棋实验报告_C#实现五子棋游戏
  4. 免费的数据同步备份软件FileYee
  5. Linux系统中关闭google浏览器更新提示
  6. parallels for linux,教大家Parallels desktop安装linux教程
  7. http协议没有已注册的程序
  8. 隐私计算的介绍-入门学习笔记(一)
  9. 用Matlab实现车牌分割(可识别大部分蓝色、绿色车牌)
  10. 打死我也不说(深度优先搜索)