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

题目大意是给你一个字符串s,每回取s的头或尾构成字符串t,使t的字典序最小
用贪心法求解,注意题目要求每行最多输出80个字符
 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
char s[], t[];
int n;
int cmp(int a, int b) {
while(s[a] == s[b] && a < b) {
a++;
b--;
}
return s[a] - s[b]; }
void show() {
int len = strlen(t);
int ct = ;
for(int i = ; i < len; i++) {
printf("%c",t[i]);
ct++;
if(ct == ) {
printf("\n");
ct = ;
}
}
printf("\n");
}
int main(int argc, char const *argv[])
{
//freopen("input.txt","r",stdin);
while(scanf("%d",&n) != EOF) {
for(int i = ; i < n; i++) {
scanf("%s",&s[i]);
}
s[n] = '\0';
int ptr = , qtr = n-;
for(int i = ; i < n; i++) {
int cp = cmp(ptr, qtr);
if(cp <= ) {
t[i] = s[ptr];
ptr++;
}
else if(cp > ) {
t[i] = s[qtr];
qtr--;
}
}
t[n] = '\0';
show();
}
return ;
}

最新文章

  1. AES —— JAVA中对称加密和解密
  2. NetBeans使用习惯:升级与保存配置
  3. 在ubuntu上搭建开发环境6---安装和使用vim及其插件(Pathogen和NERDTree)
  4. [转]Web性能监控自动化探索之路–初识WebPageTest
  5. 常用数据结构[OpenCV 笔记12]
  6. bzoj3675: [Apio2014]序列分割
  7. [Hapi.js] Extending the request with lifecycle events
  8. 学习HTML的第三次课
  9. 4.2WebHost配置「深入浅出ASP.NET Core系列」
  10. idea的一些设置
  11. Linux-信号量与P,V操作
  12. 19_04_19校内训练[Game]
  13. class in Bad version
  14. WebSocket 长连接 及超时问题解决
  15. echarts 去掉网格线
  16. rpgmakermv(6) YEP_ItemSynthesis.js物品合成插件
  17. Git 同步远程仓库
  18. Effective STL 学习笔记14: Use reserve to avoid unnecessary reallocations.
  19. 微信小程序Nginx环境配置
  20. BZOJ1718: [Usaco2006 Jan] Redundant Paths 分离的路径【边双模板】【傻逼题】

热门文章

  1. linux安装redis官方教程
  2. UIView动画效果之----翻转.旋转.偏移.翻页.缩放.取反的动画效
  3. Windows上SVN服务器搭建【转】
  4. vue+element ui项目总结点(五)Carousel 走马灯组件、Collapse 折叠面板、Tree 树形控件
  5. LibreOJ #103. 子串查找
  6. 如何用JavaScript判断前端应用运行环境(移动平台还是桌面环境)
  7. perl 引用(数组和hash引用) --- perlreftut - Mark 的一个简单的&#39;引用&#39;教程 ---Understand References Today. --Mark Jason Dominus, Plover Systems (mjd-perl-ref+@plover.com)
  8. 利用VS自带的命令行工具查看和生产PublicKeyToken
  9. 解决IIS7多域名绑定同一物理目录,设置不同的默认文档的问题
  10. hibernate4+spring3+struts2搭建框架实例