poj 3617 Best Cow Line 贪心模拟
Best Cow Line
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 42701 | Accepted: 10911 |
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 题意:输入n个大写字母组成一个字符串S,每次可以从这个字符串的队首或队尾取一个字符加到字符串T中(T初始为空),
输出字典序最小的T,当T的长度等于80时输出一个换行
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
string s,t;
char x;
int n;
int main()
{
cin>>n;
int temp=n;
while(temp--)
{
cin>>x;
s=s+x;
}
int l=,r=n-,cnt=;
while(l<=r)
{
int flag=;
for(int i=;i<=r-l;i++)
{
if(s[l+i]<s[r-i])
{
flag=;//输出左边
cnt++;
break;
}
else if(s[l+i]>s[r-i])
{
flag=;//输出右边
cnt++;
break;
}
else
{
continue;//相等,比较下一个
}
}
if(flag==)
{
cout<<s[l];
l++;
}
else
{
cout<<s[r];
r--;
}
if(cnt%==)
cout<<endl;
}
cout<<endl;
return ;
}
poj 3617 Best Cow Line 贪心模拟的更多相关文章
- 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 (模拟)
题目链接 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 (贪心)
题意:给定一行字符串,让你把它变成字典序最短,方法只有两种,要么从头部拿一个字符,要么从尾部拿一个. 析:贪心,从两边拿时,哪个小先拿哪个,如果一样,接着往下比较,要么比到字符不一样,要么比完,也就是 ...
- 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 (贪心)
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: 9284 Accepted: 2826 Des ...
- POJ 3617 Best Cow Line (字典序最小问题 & 贪心)
原题链接:http://poj.org/problem?id=3617 问题梗概:给定长度为 的字符串 , 要构造一个长度为 的字符串 .起初, 是一个空串,随后反复进行下列任意操作. 从 的头部删除 ...
- poj 3617 Best Cow Line
http://poj.org/problem;jsessionid=F0726AFA441F19BA381A2C946BA81F07?id=3617 Description FJ is about t ...
随机推荐
- leetcode: 0204 完成的
目录 大纲:0204 完成的 notes ✅1051 高度检查器 ✅ 728 自除数 brute c解答: java switch 语句 java api: array 直接有 length 属性 , ...
- 理解js中的原型链
对象有”prototype”属性,函数对象有”prototype”属性,原型对象有”constructor”属性. 关于原型 在JavaScript中,原型也是一个对象,通过原型可以实现对象的属性继承 ...
- [转]Mysql连表之多对多
转自 回到顶部 连表多对多 可以理解成一夫多妻和一妻多夫. 男人表: nid name 1 xxx 2 yyy 3 zzz 女人表: nid name 1 aaa 2 bbb 3 ccc 要让两个表建 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:设定文本对齐,段落中超出屏幕部分文字自动换行
<!DOCTYPE html> <html> <head> <title>菜鸟教程(runoob.com)</title> <meta ...
- 图像分割利用KMeans生成灰度图
import numpy as np import PIL.Image as image from sklearn.cluster import KMeans def loadData(filePat ...
- Java基础 -4.5
循环控制 在循环语句定义的时候还有两个控制语句:break.continue break主要的功能是退出整个循环结构 continue严格来讲只是结束当前的一次调用(结束当前循环) 当执行到了cont ...
- 02-07Android学习进度报告七
今天主要学习了关于Android开发的Date和Time组件部分内容. 首先看TextClock: 可以通过调用:TextClock提供的is24HourModeEnabled()方法来查看,系统是否 ...
- 【PAT甲级】1023 Have Fun with Numbers (20 分)
题意: 输入一个不超过20位的正整数,问乘2以后是否和之前的数组排列相同(数字种类和出现的个数不变),输出Yes或No,并输出乘2后的数字. AAAAAccepted code: #define HA ...
- freemarker作为PDF模板实现下载功能
freemarker 文件 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type ...
- Spring MVC原理解析
SpringMVC Spring MVC的工作原理 ①客户端的所有请求都交给前端控制器DispatcherServlet来处理,它会负责调用系统的其他模块来真正处理用户的请求. ② Dispatche ...