Best Cow Line
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10605   Accepted: 3189

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

Source

USACO 2007 November Silver
题意:给定长度为n的str字符串 要构筑一个新的字符串t,开始t是个空串
两个操作:
1.将第一个字符加在新字符串尾部

2.将最后一个字符加在新串尾部

使得新串最小

ps; 每80为一个新串

字典序:是指从前到后比较两个字符串大小的方法。首先比较第一个字符大小,如果不同则第一个字符小的字符串更小,如果相同则比较第二个、、、

如此继续。。比较谁大谁小

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;char str[3005];
int n;
void solve()
{int a = 0,b=n-1;int cnt=0;while(a<=b){bool left = true ;for(int i=0; a+i<=b; i++){if(str[i+a]<str[b-i]){left = true ;break;}else if(str[i+a]>str[b-i]){left = false;break;}}if(left)putchar(str[a++]);elseputchar(str[b--]);cnt++;if(cnt==80){cout<<endl;cnt=0;}}cout<<endl;
}
int main()
{while(cin>>n){for(int i=0;i<n;i++) cin>>str[i];str[n]='\0';solve();}
}

贪心字典序最小问题poj3617相关推荐

  1. 【挑战程序设计竞赛】- 2.2贪心法(硬币最少、区间覆盖、字典序最小、标记最少、木板切割)

    四年前犯的错再做一遍还是会犯. 四年前不看presentation要求,四年后依然PE. 四年前忘记longlong,四年后还是会忘. 2.2 贪心法 核心思想:不断选取最优策略. 例题1-硬币:有1 ...

  2. 贪心算法——字典序最小问题

    2019独角兽企业重金招聘Python工程师标准>>> 贪心算法--字典序最小问题 问题主题:字典序最小 问题描述: 给定长度为N的字符串S,要构造一个长度为N字符串T.T是一个空串 ...

  3. 字典序最小问题(贪心)

    题目大意:输入n,代表有一个长度为n的字符串. 起初,T是一个空串,随后反复进行下列任意操作: 从S 的头部删除一个字符,加入到T的尾部' 从S的尾部删除一个字符,加入到T的尾部 目的是构造字典序尽可 ...

  4. HDU 6301.Distinct Values-贪心、构造字典序最小的数列 (2018 Multi-University Training Contest 1 1004)...

    HDU6301.Distinct Values 这个题就是给你区间要求区间内的数都不相同,然后要求是字典序最小,直接贪心走一遍,但是自己写的时候,思路没有错,初始化写挫了... 将区间按左端点小的排序 ...

  5. 中石油训练赛 - Swapping Places(字典序最小的拓扑排序)

    题目链接:点击查看 题目大意:给出 s 个字符串表示种类,再给出 m 个朋友关系,表示两个种类的动物是朋友,现在给出一个长度为 n 的种类排列,规定相邻两个是朋友的种类的动物可以交换位置,问如何操作, ...

  6. 第314场Leetcode周赛|使用机器人打印字典序最小的字符串

    题目: 给你一个字符串 s 和一个机器人,机器人当前有一个空字符串 t .执行以下操作之一,直到 s 和 t 都变成空字符串: 删除字符串 s 的 第一个 字符,并将该字符给机器人.机器人把这个字符添 ...

  7. 背包问题 输出方案、输出字典序最小方案、可行方案数、最优方案总数

    原文地址:https://blog.csdn.net/ronaldo7_zyb/article/details/81069906 1. 要求输出方案: 以01背包为例 我们可以知到,01背包用一维数组 ...

  8. UVA116 单向TSP Unidirectional TSP(多阶段决策问题、输出字典序最小的方案、DAG上DP)

    整理的算法模板合集: ACM模板 题目传送门 简单的多阶段决策问题. 多段图DAG,其节点可以划分为若干个阶段,每一个阶段只由上一个阶段所决定. 因为本题要求输出从前到后的方案,所以我们转移方程的时候 ...

  9. UVA10537 The Toll! Revisited (思维、最短路、输出字典序最小路径)

    整理的算法模板合集: ACM模板 题目链接 我们要注意去的时候交的n/20单位的货物,但是倒着回去求答案的时候我们要加上ceil(n/19.0)(自己手算) 字典序中大写字母比小写字母更小 注意看数据 ...

  10. 字符串互换后字典序最小

    问题:给出一个字符串,以及可以互换的位置对,求出互换后字典序最小的. 比如给出字符串dcab,以及可以互换的位置对[[0,3],[1, 2],[0, 2]],则互换后字典序最小的是abcd 思路:第一 ...

最新文章

  1. python在内存中生成Zip文件!
  2. python cmd命令大全-详解python调用cmd命令三种方法
  3. 固定资产制作修理费应如何入账
  4. jdbc就是这么简单
  5. C++阶段01笔记01【C++初识(第一个C++程序、注释、变量、常量、关键字、标识符命名规则)】
  6. ML.NET Cookbook:(20)我如何定义自己的数据转换?
  7. 《转》VC++多线程编程
  8. MFC开发IM-第十五篇、打包的MFC程序别人无法启动的原因
  9. 求职招聘市场复苏!市场平均薪资超8000元
  10. HttpWebRequest 忽略证书
  11. 央行降息开始 房价下跌不行也要行
  12. vim 对齐插件Tabular使用
  13. 基于Java的Office 系列文档处理五种工具简单介绍
  14. 病毒手动查杀(威金 q盗 熊猫烧香)
  15. 人生苦难重重,如何解决人生的问题?| 读《少有人走的路:心智成熟的旅程》
  16. SourceTeee 设置拉取时使用rebase
  17. CDH 使用内部Parcel仓库
  18. 如何讲好FISHER确切概率法
  19. 【Games101】图形的基础变换 总结
  20. 如何获取微软官方原版Windows 10 ISO镜像文件

热门文章

  1. 为什么要使用Keil MDK-ARM中间件库?
  2. 批量杀死MySQL连接
  3. 第三章项目管理过程重点--转载
  4. 剑指offer、按之字形打印二叉树(python)
  5. 初识BoundSQL
  6. Java-斗地主小游戏洗牌发牌(控制台程序)
  7. ARM开发6.3.1 基础实训( 1 ) 单个数码 LED 的显示输出系统设计( 1)--LPC21XX
  8. java mp4转码 h264_10分钟学会Jave视频转码avi--gt;mp4(h264编码格式)!_Java_七九推...
  9. linux 终止作业任务命令,linux-尝试终止进程时出错—“ kill:pid:参数必须是进程或作业ID”...
  10. html基础 — area(图像的作用区域标记)