Best Cow Line
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26670   Accepted: 7226

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

思路:从左Left和从右Right同时开始扫,如果如果左边的值小于右边的值,则输出左边的,Left++。
  如果左边的值大于右边的值,则输出右边的,Right++。
  如果左边的值等于右边的值,则比较从左往右形成的字符串和从右往左形成的字符串哪个更小。输出小的那边的,改变Left或者Right。 代码:
#include <iostream>
using namespace std;
typedef long long ll; int n;
char a[]; int main() {
cin >> n;
for(int i = ;i < n; i++) cin >> a[i]; string s; int left = ;int right = n-;
while(left <= right){
int l = false;
for(int i = ;i <= right - left; i++){
if(a[left+i] < a[right-i]){
l = true;
}
if(a[left+i] != a[right-i]){
break;
}
} if(l) s += a[left++];
else s += a[right--];
}
for(int i = ;s[i]; i++){
if(i != &&i% == ) cout << endl;
cout << s[i];
} return ;
}
// writen by zhangjiuding
 

POJ 3617 Best Cow Line 贪心算法的更多相关文章

  1. poj 3617 Best Cow Line 贪心模拟

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42701   Accepted: 10911 D ...

  2. POJ 3617 Best Cow Line (贪心)

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

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

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

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

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

  5. poj 3617 Best Cow Line (字符串反转贪心算法)

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9284   Accepted: 2826 Des ...

  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 (模拟)

    题目链接 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Yea ...

  9. poj 3617 Best Cow Line

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

随机推荐

  1. nginx令牌限制并发

    http{ limit_req_zone $binary_remote_addr zone=req_one:10m rate=100r/s; server { listen 8080; server_ ...

  2. js滑动提示效果

    js代码 漂亮的动画效果:在靠右上角:背景颜色为红,字体颜色为白色  滑动 变大 上移  缓慢渐变消失 function tishi() { $("#tishi").attr(&q ...

  3. es6总结(二)

    1.es6三种声明方式: a.var 全局声明 b.let  局部变量声明 c.const     常量声明 2.变量的解构赋值 a.数组的解构赋值 等号左边与右边形式统一  let [a,[b,c] ...

  4. MyBatis数据持久化(十)与Spring4整合

    前面几节介绍了mybatis的基本使用方法,本节主要介绍如何使用mybatis与主流的IoC容器Spring进行整合. 我们首先需要获取Spring框架的jar文件,在写本文时spring的最新Rel ...

  5. Win8 Windows Defender default behaviour

    Problem Description ********************* Is it possible, to change the default behaviour when findi ...

  6. JS使用三元运算符判断三个数中最大的数

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. java开发移动端之spring的restful风格定义

    https://www.ibm.com/developerworks/cn/web/wa-spring3webserv/index.html

  8. Jmeter中模拟多用户执行多场景操作

    1.其实一个用户组就是一个场景(Thread Group).可以在一个测试计划中进行多个场景的执行,在测试计划下加一个全局的User Defined Variables,在这个里面可以设置执行总数to ...

  9. 使用sourceMap文件定位小程序错误信息

    sourceMap是什么 在前端开发过程中代码难免会有错误,即便是再小心,也有可能出现 Cannot read property 'xxx' of null 这样的低级失误,debug自然是家常便饭. ...

  10. Java web课程学习之Request和Response

    request和response l HttpServletRequest l 请求转发 l HttpServletResponse l 请求重定向   请求流程 每次请求service(),都会由容 ...