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

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<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<queue>
using namespace std;
char s[],s1[];
int compare(int x,int y,int t)
{
//cout<<x<<"**"<<y<<endl;
//cout<<"*"<<s[1]<<"*"<<endl;
// cout<<s[x]<<"^^"<<s1[y]<<endl;
int j=;
while(j<=t)
{ if(s[x]<s1[y])
return ;
else if(s[x]>s1[y])
return ;
x++;y++;j++;
}
return ;
}
int main()
{
int i,j,k,t,m,n;
while(cin>>t)
{
for(int i=,j=t-;i<t;i++,j--)
{
cin>>s[i];
s1[j]=s[i];
}
n=t;
int x=,y=,ans=;
while(n--)
{
int kk=compare(x,y,n);
if(kk== || kk==)
{
cout<<s[x];
x=x+;
}
else if(kk==)
{
cout<<s1[y];
y=y+;
}
ans++;
if(ans%==)
cout<<endl;
}
}
return ;
}

poj 3617 Best Cow Line (字符串反转贪心算法)的更多相关文章

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

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

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

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

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

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

  4. POJ 3617 Best Cow Line (贪心)

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

  5. POJ 3617 Best Cow Line 贪心算法

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

  6. poj 3617 Best Cow Line 贪心模拟

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

  7. poj 3617 Best Cow Line

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

  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 (贪心)

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

随机推荐

  1. python logging模块用法

    http://blog.csdn.net/zyz511919766/article/details/25136485/ import logging logging.debug('debug mess ...

  2. zigbee 学习笔记

    在德州仪器的站点:http://www.ti.com.cn/tool/cn/z-stack上下载安装zigbee2007协议栈版,我的是ZStack-CC2530-2.3.0-1.4.0. 以下演示一 ...

  3. python对于0x01的处理

    对于python脚本,可以使用: .replace('\x01', '') 对于vim工具,可以使用: :%s/\%x01/ /g

  4. springmvc实现简单的拦截器

    SpringMVC 中的Interceptor 拦截请求是通过HandlerInterceptor 来实现的.在SpringMVC 中定义一个Interceptor 非常简单,主要有两种方式,第一种方 ...

  5. 你以为border-radius只是圆角吗?【各种角度】

    border-radius,国内翻译成圆角,你可能以为这个属性就是用来画圆角,没错,但是除此之外,它还可以做点别的事情. radius其实指的是边框所在圆的半径,这个CSS3属性不仅能够创建圆角,还可 ...

  6. Ajax的简单总结

    1. Ajax的优势和不足 1.1 Ajax的优势 1. 不需要插件支持 Ajax不需要任何浏览器插件,就可以被绝大多数主流浏览器所支持,用户只需要允许JavaScript在浏览器上执行即可. 2. ...

  7. MySQL : ERROR 1042 (HY000): Can't get hostname for your address

    摘自: http://www.siutung.org/post/506/ 使用Navicat for MySQL连接远程的MySQL服务器,却提示:Can't get hostname for you ...

  8. Hibernate关系映射(三) 多对一和一对多

    一.多对一 学生Student和班级Grade实现多对一,多个学生对应一个班级. Student.java实体类,映射了班级的属性. package com.lxit.entity; import j ...

  9. iOS/iphone开发如何为苹果开发者帐号APPID续费

    原文地址:iOS/iphone开发如何为苹果开发者帐号APPID续费作者:陈双超_群雄 其实相当的简单,这篇内容是给财务看的,有的地方连我自己看了都感觉有点...但如果不详细,她又要为难我,所以我就当 ...

  10. 算法笔记_163:算法提高 最大乘积(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 对于n个数,从中取出m个数,如何取使得这m个数的乘积最大呢? 输入格式 第一行一个数表示数据组数 每组输入数据共2行: 第1行给出总共的数 ...