poj 3617 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 (字符串反转贪心算法)的更多相关文章
- POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心
带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...
- POJ 3617 Best Cow Line(最佳奶牛队伍)
POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...
- POJ 3617 Best Cow Line (字典序最小问题 & 贪心)
原题链接:http://poj.org/problem?id=3617 问题梗概:给定长度为 的字符串 , 要构造一个长度为 的字符串 .起初, 是一个空串,随后反复进行下列任意操作. 从 的头部删除 ...
- POJ 3617 Best Cow Line (贪心)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted: 4 ...
- POJ 3617 Best Cow Line 贪心算法
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26670 Accepted: 7226 De ...
- poj 3617 Best Cow Line 贪心模拟
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42701 Accepted: 10911 D ...
- poj 3617 Best Cow Line
http://poj.org/problem;jsessionid=F0726AFA441F19BA381A2C946BA81F07?id=3617 Description FJ is about t ...
- 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 ...
- POJ 3617 Best Cow Line (贪心)
题意:给定一行字符串,让你把它变成字典序最短,方法只有两种,要么从头部拿一个字符,要么从尾部拿一个. 析:贪心,从两边拿时,哪个小先拿哪个,如果一样,接着往下比较,要么比到字符不一样,要么比完,也就是 ...
随机推荐
- 关于Java设计模式的一些概况
设计模式(Design pattern)在软件行业一直都扮演着很重要的角色.最近感觉自己对设计模式的知识有些遗忘了,虽然以前也看了很多,但是坦白说,其实并没有怎么理解.基本还是为了应付面试.然后,在工 ...
- 向PE文件植入后门代码技术讨论
写在前面的话 这篇文章将介绍使用codecaves对PE文件植入后门代码.有几个很好的工具可以帮到你了.比如BackdoorFactory和Shelter将完成相同的工作,甚至绕过一些静态分析几个防病 ...
- (转)Android项目重构之路:实现篇
前两篇文章Android项目重构之路:架构篇和Android项目重构之路:界面篇已经讲了我的项目开始搭建时的架构设计和界面设计,这篇就讲讲具体怎么实现的,以实现最小化可用产品(MVP)的目标,用最简单 ...
- C++ 初始化列表(转载)
何谓初始化列表 与其他函数不同,构造函数除了有名字,参数列表和函数体之外,还可以有初始化列表,初始化列表以冒号开头,后跟一系列以逗号分隔的初始化字段.在 C++中,struct和class的唯一区别是 ...
- Telnet服务配置
telnet:远程连接,使用未加密的用户/密码组进行验证,由xinetd服务管理.配置文件为/etc/xinetd.d/telnet Telnet服务的配置步骤如下: 一.安装telnet软件包 #r ...
- Android,TextView的所有属性和方法
XML 属性 属性名称 相关方法 描述 android:autoLink setAutoLinkMask(int) 设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接. ...
- 如何在网页中嵌入QQ 阿里旺旺等代码
1 登陆以下网址: http://wp.qq.com/login.html?target=1 2 复制代码到HTML中即可 3 将对方和自己的QQ都登陆测试(注意自己QQ必须是2010以上版本,否则会 ...
- Android 文件存储 和 权限管理
转载请标明出处: :http://blog.csdn.net/huaiyiheyuan/article/details/52473984 android SD卡主要有两种存储方式 Internal . ...
- RGB和HSL色彩的相互转换
转自: http://blog.csdn.net/aniven/article/details/2205851 RGB和HSL(也叫HSB/HSV)是两种色彩空间,即:红,绿,蓝(Red,Green, ...
- 学会使用简单的MySQL操作
第十八章 学会使用简单的MySQL操作 在前面两个章节中已经介绍过MySQL的安装了.可是光会安装还不够.还须要会一些主要的相关操作.当然了,关于MySQL的内容也是非常多的.仅仅只是对于linux系 ...