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 ;
}

POJ-3617的更多相关文章

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

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

  2. POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心

    带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...

  3. Best Cow Line <挑战程序设计竞赛> 习题 poj 3617

    P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Goldpoj 3617 http://poj.org/problem?id=3617 题目描述FJ is about ...

  4. poj 3617 Best Cow Line

    http://poj.org/problem;jsessionid=F0726AFA441F19BA381A2C946BA81F07?id=3617 Description FJ is about t ...

  5. poj 3617 Best Cow Line 解题报告

    题目链接:http://poj.org/problem?id=3617 题目意思:给出一条长度为n的字符串S,目标是要构造一条字典序尽量小,长度为n的字符串T.构造的规则是,如果S的头部的字母 < ...

  6. POJ 3617 Best Cow Line (字典序最小问题 & 贪心)

    原题链接:http://poj.org/problem?id=3617 问题梗概:给定长度为 的字符串 , 要构造一个长度为 的字符串 .起初, 是一个空串,随后反复进行下列任意操作. 从 的头部删除 ...

  7. POJ 3617 Best Cow Line (贪心)

    Best Cow Line   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted: 4 ...

  8. POJ 3617 Best Cow Line (贪心)

    题意:给定一行字符串,让你把它变成字典序最短,方法只有两种,要么从头部拿一个字符,要么从尾部拿一个. 析:贪心,从两边拿时,哪个小先拿哪个,如果一样,接着往下比较,要么比到字符不一样,要么比完,也就是 ...

  9. Best Cow Line (POJ 3617)

    题目: 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下列任意操作. ·从S的头部删除一个字符,加到T的尾部 ·从S的尾部删除一个字符,加到T的尾部 目标是要构 ...

  10. POJ 3617:Best Cow Line(贪心,字典序)

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

随机推荐

  1. 早一天把生意做到de

    const isMobilePhoneNum = (params) => { const areaCode = params.areaCode || 'cn' const str = param ...

  2. 360手机助手: App上架(提交资料)注意事项

    提交app资料尤其注意: .keystore和密码?由HBuilder提供公有证书: 签名证书KeyStore证书别名:hbuilder证书密码:123456keystore密码:123456 360 ...

  3. mysql系列之8.mysql高可用 (mha4mysql)

    环境: 三台机器 主服务器: 192.168.1.130 主备机器: 192.168.1.131 监控机器: 192.168.1.132 130和131, 是mysql双主架构 1.在三台机器上安装m ...

  4. 洛谷 3275 [SCOI2011]糖果

    题目戳这里 N句话题意 有N个人,k个限制,有五种限制 如果X=1, 表示第A个小朋友的糖果必须和第B个小朋友的糖果一样多: 如果X=2, 表示第A个小朋友的糖果必须少于第B个小朋友的糖果: 如果X= ...

  5. VM tools安装错误The path "" is not a valid path to the xx generic kernel headers.

    VMWARE TOOLS安装提示THE PATH IS NOT A VALID PATH TO THE GENERIC KERNEL HEADERSI solved this problem, I g ...

  6. spring 配置bean-自己主动装配

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qilixiang012/article/details/28260477 概要:(蓝色为本节所讲) ...

  7. smartforms 二维码打印

    1. 安装TBarCode_SAPwin软件 1) 下载Barcode软件  下载TBarCode_SAPwin 软件.(如需生成SAP"字符控制序列"则需一并下载TBarCode ...

  8. 转载:SPFA算法学习

    转载地址:http://www.cnblogs.com/scau20110726/archive/2012/11/18/2776124.html 粗略讲讲SPFA算法的原理,SPFA算法是1994年西 ...

  9. 一小时搞明白自定义注解(Annotation)

    原文链接:http://blog.csdn.net/u013045971/article/details/53433874 什么是注解 Annotation(注解)就是Java提供了一种元程序中的元素 ...

  10. 分享知识-快乐自己:IDEA下maven编译打包Java项目成jar包但是resource下配置文件无法编译

    今天在写分布式项目的时候,一直无法编译 resource 下的配置文件:(在target文件夹下的 classes文件查看是否编译) 最后只能通过在POM文件中配置resources配置 得以解决: ...