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. Openshift 用户,角色和RBAC

    OCP中的权限管理沿用的Kubernetes RBAC机制,授权模式主要取决于下面几个因数 Rules 针对主要对象的操作权限,比如建立Pod Sets of permitted verbs on a ...

  2. linux面试题目—2

    linux面试题目—2 二 选择题 1.关闭linux系统(不重新启动)可使用命令 B . A Ctrl+Alt+Del B halt C shutdown -r now D reboot 2.实现从 ...

  3. 二十四种设计模式:单例模式(Singleton Pattern)

    单例模式(Singleton Pattern) 介绍保证一个类仅有一个实例,并提供一个访问它的全局访问点. 示例保证一个类仅有一个实例. Singleton using System; using S ...

  4. [转]Configure a Package to Use Transactions SSIS

    本文转自:http://msdn.microsoft.com/en-us/library/ms141144.aspx When you configure a package to use trans ...

  5. uv计算

    lightmap shadowmap heightmap 它们有一个自己的camera 对应cameraMatrix float3 TransfromToTextureCoord(float4 Pos ...

  6. [ES6] 08. Destructuring Assignment -- 1

    Here is the way you get value from an object: var obj = { color: "blue" } console.log(obj. ...

  7. 机器学习基石第一讲:the learning problem

    博客已经迁移至Marcovaldo's blog (http://marcovaldong.github.io/) Andrew Ng的Machine Learning比較简单,已经看完.林田轩的机器 ...

  8. Android系统常用的权限

    开机自动允许  android.permission.RECEIVE_BOOT_COMPLETED,允许程序开机自动运行. 电量统计  android.permission.BATTERY_STATS ...

  9. Java Nio Socket通讯

    Server端: #############服务器端连接请求处理############### public class MultiplexerServer implements Runnable { ...

  10. ffplay的快捷键以及选项 FFmpeg 基本用法 FFmpeg常用基本命令 ffmpeg常用转换命令,支持WAV转AMR

    ffmpeg -i 32_mkv_h264_718x480_ac3.mkv  -codec copy -bsf:v h264_mp4toannexb  -f mpegts xx.ts ./ffmpeg ...