Best Cow Line
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 25616   Accepted: 6984

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

题意:

对于与给出的字母,将它们按字典序最小的顺序排列输出。

采用 贪心的思想,每次取字典序最小的一个字母。

若首位字母相同,则向内收缩比较下一个,直到找出最小的。

AC代码:

 //#include<bits/stdc++.h>
#include<iostream>
#include<stdio.h>
#include<String.h>
using namespace std; int main(){
ios::sync_with_stdio(false);
int n;
cin>>n;
char s[n+];
for(int i=;i<n;i++){
cin>>s[i];
}
int a=,b=n-,ans=;
while(a<=b){
int flag=;
for(int i=;a+i<=b;i++){//若首位相等则比较下一个
if(s[a+i]<s[b-i]){
flag=;
break;
}
if(s[a+i]>s[b-i]){
flag=;
break;
}
}
if(flag){
cout<<s[a++];
ans++;
}
else{
cout<<s[b--];
ans++;
}
if(ans==){
ans=;
cout<<endl;
}
}
cout<<endl;
return ;
}

最新文章

  1. HashSet源码详解
  2. sprintf的缓冲区溢出问题
  3. 与众不同 windows phone (50) - 8.1 新增控件: PickerFlyout, ListPickerFlyout
  4. linux 中文件夹的文件按照时间倒序或者升序排列
  5. 通过ros节点发布Twist Messages控制机器人--10
  6. leetcode 19
  7. hdu 4725 最短路
  8. Java模拟网站登录02【转载】
  9. phpcmsv9全站搜索,不限模型
  10. 【转】基本数据持久性(一) 使用plist保存和读取数据
  11. SQL 插入查询的最大ID 号 进行批量
  12. linux三剑客之sed命令
  13. FixedUpdate真的是固定的时间间隔执行吗?聊聊游戏定时器
  14. springboot集成rabbitmq(实战)
  15. blinker库
  16. Windows操作系统下搭建Git服务器和客户端。
  17. Kubernetes理论基础
  18. lavarel mongo 操作
  19. [HNOI2005]狡猾的商人 ,神奇做法——贪心
  20. sql server 我常用的语句

热门文章

  1. 【BZOJ3679】数字之积 DFS+DP
  2. 基于EasyNVR二次开发实现业务需求:直接集成EasyNVR播放页面到自身项目
  3. kafka source type
  4. Swift 学习笔记 (闭包)
  5. QQ登录集成到自己网站php代码(转载)
  6. 【ELK】Elasticsearch的备份和恢复
  7. 剑指Offer:把数组排成最小的数【45】
  8. System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration sect
  9. 创建Django博客的数据库模型
  10. 尚学堂xml学习笔记