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. Executors几种常用的线程池性能比较

    java编程中,经常会利用Executors的newXXXThreasPool生成各种线程池,今天写了一小段代码,简单测试了下三种常用的线程池: import com.google.common.ut ...

  2. springboot @ConfigurationProperties @EnableConfigurationProperties @Bean @ Component

    https://www.cnblogs.com/duanxz/p/4520571.html https://juejin.im/post/5cbeaa26e51d45789024d7e2 1. Bea ...

  3. iOS 打包Framework包含其他Framework的问题

    当你打包一个framework静态库包含另一个第三方静态库时,在工程中使用自己打包的framework,这时编译会出现报错,报错的大概原因是没有找到你打包framework里面的第三方framewor ...

  4. The connection between feature spaces and smoothness is not obvious, and is one of the things we’ll discuss in the course.

    http://www.gatsby.ucl.ac.uk/~gretton/coursefiles/lecture4_introToRKHS.pdf

  5. Wix Burn:如何将32位和64位的安装包制作成一个安装包

    由于Windows Installer不是平台独立的(即区分32-bit和64-bit),因此用Wix制作的安装包在编译不能像.net应用那样采用Any CPU编译,而必须制定是目标Platform是 ...

  6. JavaScript for循环实现表格隔行变色

    本代码主要演示的是for循环, <!doctype html> <html lang="en"> <head> <meta charset ...

  7. vscode 中 eslint 的配置

    上个博客说到了如何配置 eslint 和 editorconfig, 但是每次运行项目的时候才能知道哪段代码不符合规范,确实也有点蛋疼. 那么能不能直接在编辑器中就能看到不符合规范的代码的呢??? 这 ...

  8. ssh免密登陆服务器

    本文介绍的是以公钥认证的方式实现 ssh 免密码登陆远程服务器. 客户端生成RSA公钥和私钥 在用户更目录有一个 .ssh 的文件夹,如果没有就新建一个.在文件夹中通过命令 ssh-keygen -t ...

  9. Linux Shell Script目录

    目录 Linux Shell基础 开始Shell编程 代码 示例代码查看:https://github.com/Furzoom/demo-C/tree/master/src/shell

  10. tomcat 1)启动时不识别执行启动命令 2)启动报错 3)关闭不了,用myEclipse启动时显示jvm_bind,端口占用