Best Cow Line---POJ 3617(贪心)
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
*这题主要是用贪心,目标主要是从已知的字符串构造出字典序尽可能小的字符串
*从字典序性质来看,无论字符串末尾有多大,只要保证前面部分较小就可以咯!
*假设原来字符串为S,目标字符串为T,那么,不断取出S的开头和末尾较小的一个
*字符放到T的末尾。上面的没有针对开头与结尾相同,如果,遇到这样动情况,应
*该比较下一个字符的大小。如果,下一个字符也相同,那么可以得到下面的算法:
*按照字典序比较S和将S反转后的字符串S’
*如果,S较小,那么取出S开头字符,追加到T的末尾
*如果,S'较小,那么取出S结尾字符,追加到T的末尾
*如果,相同则取出那一个都可以。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn =;
int n;
char s[maxn]; int main(){
cin>>n;
for( int i=; i<n; i++ ){
cin>>s[i];
}
int a=,b=n-;
int cnt=;
while(a<=b){
bool left=false;
for( int i=; a+i<=b; i++ ){
if(s[a+i]<s[b-i]){
left=true;
cnt++;
break;
}
else if(s[a+i]>s[b-i]){
left=false;
cnt++;
break;
}
}
if(left) putchar(s[a++]);
else putchar(s[b--]);
if(cnt%==) putchar('\n');
}
return ;
}
Best Cow Line---POJ 3617(贪心)的更多相关文章
- Best Cow Line (POJ 3617)
题目: 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下列任意操作. ·从S的头部删除一个字符,加到T的尾部 ·从S的尾部删除一个字符,加到T的尾部 目标是要构 ...
- 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 (字符串反转贪心算法)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9284 Accepted: 2826 Des ...
- Best Cow Line(POJ No.3617)
问题: 链接:http://poj.org/problem?id=3617 思路: 按照字典序比较S和将S反转后的字符串S' 如果S较小,就从S的开头取出一个字符,加到T的末尾(更新下标值) 如果S’ ...
- Best Cow Line (POJ 3217)
给定长度为N的字符串S,要构造一个长度为N的字符串T,起初,T是一个空串,随后反复进行下列任意操作. *从S的头部删除一个字符,加到T的尾部 *从S的尾部删除一个字符,加到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: 30684 Accepted: 8185 De ...
- 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 ...
- Best Cow Line <挑战程序设计竞赛> 习题 poj 3617
P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Goldpoj 3617 http://poj.org/problem?id=3617 题目描述FJ is about ...
随机推荐
- zabbix监控ssl证书到期时间
监控脚本 cat ssl_check.sh #!/bin/bash # #获取ssl证书的过期时间 #menghao #获取证书的有效时间 time=$(echo | openssl s_client ...
- Kubernetes的十大使用技巧
1. bash针对kubectl命令的自动补充这可能是在使用Kubernetes过程中最容易做的事,但它也是其中一个最有用的.要添加自动补充功能,如果使用bash,只需执行以下命令: echo &qu ...
- SQLAlchemy使用(三)搭配Flask框架使用
前言 本章应该是SQLAlchemy使用系列的最后一篇了,本章简单讲一下如何搭配Flask使用.下一篇应该是写Flask_restful相关内容了 正文 我们简单使用前两章的model,两张表 # - ...
- 成环的概率dp(初级) zoj 3329
原题地址:https://vjudge.net/problem/ZOJ-3329 题目大意: 有三个骰子,分别有k1,k2,k3个面,初始分数是0.第i骰子上的分数从1道ki.当掷三个骰子的点数分别为 ...
- selenium对百度进行登录注销
#百度登录退出demo import time from selenium import webdriver from selenium.webdriver.common.action_chains ...
- scrapy爬取数据保存csv、mysql、mongodb、json
目录 前言 Items Pipelines 前言 用Scrapy进行数据的保存进行一个常用的方法进行解析 Items item 是我们保存数据的容器,其类似于 python 中的字典.使用 item ...
- Redis数据类型Set
Redis的SET是无序的String集合,它里面的元素是不会重复的. SADD,SMEMBERS SADD命令会添加新元素到SET,可以看到一次性可以添加一个或多个元素.SMEMBERS可以获取se ...
- WPF自定义轮播控件
闲得蛋疼做了一个WPF制作轮播动画(随机动画),勉强可以看,写个随笔留个脚印. 效果图:
- .net core 2.x - ids4 - identity - two factory 登录认证
本片内容使用到ids4+ids4.Entityframework持久化表单,以及core的identity相关表的一并持久化,然后就是登录认证,认证使用email发送邮件的方式.所以这里涉及到四块内容 ...
- [原创]windows 部署SS server 出现的错误.
安装过程: .Download and install Python MSI installer in 64bit Windows. .During installation you should i ...