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. CSS -- 文字竖直居中

    元素的height 和 lineheight 设置为一样即可.

  2. jquery dialog close icon missing 关闭图片丢失,样式丢失问题

    http://stackoverflow.com/questions/17367736/jquery-ui-dialog-missing-close-icon

  3. iOS:使用block代码块实现事件处理过程中的回调

    block是什么,这里就不多加强调了,它的优点: 第一:执行效率高,速度快 第二:使用起来比代理简单,省却不少代码,增强代码美感 有一些小的知识点要强调一下: 第一点:它类似于一个匿名函数,也跟jav ...

  4. JStorm模型设计

    问题描述 1.在流式计算中经常需要对一批的数据进行汇总计算,类似SQL中的GROUP BY.在用JStorm来实现这一条简单的SQL时,面对的是一条一条的数据库变化的消息(这里需要保证有序消费),其实 ...

  5. (C语言版)链表(四)——实现双向循环链表创建、插入、删除、释放内存等简单操作

    双向循环链表是基于双向链表的基础上实现的,和双向链表的操作差不多,唯一的区别就是它是个循环的链表,通过每个节点的两个指针把它们扣在一起组成一个环状.所以呢,每个节点都有前驱节点和后继节点(包括头节点和 ...

  6. ray tracing/shadow,reflection, caustic

    看了一下午终于明白raytracing的算法了 不知道这次能记住多久 ssr我又完全不记得了 按照Henrik所说 理解raytracing的核心在于,它是从Eye到light反着走的 需要一个前序的 ...

  7. ISP图像调试工程师——tone Mapping(ISP)

    http://www.cnblogs.com/bigbigtree/p/3458797.html

  8. SparkSQL之更改表结构

    本文篇幅较短,内容源于自己在使用SparkSQL时碰到的一个小问题,因为在之后的数据处理过程中多次使用,所以为了加深印象,在此单独成文,以便回顾. 场景 在使用SparkSQL进行数据处理时,碰到这样 ...

  9. DataGridView绑定泛型List时,利用BindingList来实现增删查改

    DataGridView绑定泛型List时,利用BindingList来实现增删查改  一.   DataGridView绑定泛型List的种种 1.DataGridView数据绑定对比(DataTa ...

  10. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-有时候项目会无法编译,重新生成就自动卡死或者自动退出怎么办

    删除所有中文注释,有中文注释则不一定能编译成功.     更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetaohai123   我的在线论坛: ...