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. Spark RDD概念学习系列之如何创建Pair RDD

    不多说,直接上干货! 创建Pair RDD Python语言 pairs = lines.map(lambda x: (x.split(], x))  scala语言 val pairs = line ...

  2. DIV水平方向居中的几种方法

    一.使用margin: 1 #center0 { 2 background: red; 3 margin: 0 auto; 4 } 或者: margin: auto; 这样的前提是父盒子里没有其他盒子 ...

  3. 清北集训Day1T3 LYK loves jumping(期望DP)

    题目描述 LYK在玩一个魔法游戏,叫做跳跃魔法. 有n个点,每个点有两个属性hi和ti,表示初始高度,和下降高度.也就是说,它初始时高度为hi,一旦LYK踩在这个点上,由于重力的影响,这个点的高度会下 ...

  4. MySQL · 答疑解惑 · 备库Seconds_Behind_Master计算

    背景 在mysql主备环境下,主备同步过程如下,主库更新产生binlog, 备库io线程拉取主库binlog生成relay log.备库sql线程执行relay log从而保持和主库同步. 理论上主库 ...

  5. HDU 2303 The Embarrassed Cryptographer

    The Embarrassed Cryptographer 题意 给一个两个素数乘积(1e100)K, 给以个数L(1e6), 判断K的两个素数是不是都大于L 题解 对于这么大的范围,素数肯定是要打表 ...

  6. IOS - IOS之同步请求、异步请求、GET请求、POST请求(转载)

    转载:http://www.open-open.com/lib/view/open1355055986679.html 1.同步请求可以从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务 ...

  7. webstorm狂吃内存的解决方法

    今天使用webstorm,电脑居然卡死了,我的电脑配置: 运行内存16g,1.5T内存的台式, 后来发现,可以通过设置 内存值大小来解决. 具体办法: 找到WebStorm.exe.vmoptions ...

  8. AMD包下载及使用

    AMD下载 下载地址 选择837.zip下载即可,将837.zib考入相应的文件夹下,并解压缩 wget http://netlib.org/toms/837.zip unzip 837.zip cd ...

  9. 紫书 例题8-1 UVa 120(构造法)

    #include<cstdio> #include<iostream> #include<sstream> #include<algorithm> #d ...

  10. Unity 常用常找的东西存放

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/50483316 作者:car ...