POJ 3617 Best Cow Line 贪心算法

Best Cow Line
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 26670 Accepted: 7226

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






思路:从左Left和从右Right同时开始扫,如果如果左边的值小于右边的值,则输出左边的,Left++。
  如果左边的值大于右边的值,则输出右边的,Right++。
  如果左边的值等于右边的值,则比较从左往右形成的字符串和从右往左形成的字符串哪个更小。输出小的那边的,改变Left或者Right。



代码:
#include <iostream> 
using namespace std;
typedef long long ll;int n;
char a[2010];int main() {cin >> n;for(int i = 0;i < n; i++) cin >> a[i];string s;int left = 0;int right = n-1;while(left <= right){int l = false;for(int i = 0;i <= right - left; i++){if(a[left+i] < a[right-i]){l = true;}if(a[left+i] != a[right-i]){break;}}if(l) s += a[left++];else s += a[right--];} for(int i = 0;s[i]; i++){if(i != 0&&i%80 == 0) cout << endl;cout << s[i];}return 0;
}
//  writen by zhangjiuding

 

 

转载于:https://www.cnblogs.com/zhangjiuding/p/7663724.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/399592.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Ubuntu升级软件和ubuntu升级系统的命令

sudo apt-get update: 升级安装包相关的命令,刷新可安装的软件列表(但是不做任何实际的安装动作) sudo apt-get upgrade: 进行安装包的更新(软件版本的升级) sudo apt-get dist-upgrade: 进行系统版本的升级(Ubuntu版本的升级) sudo do-release-upgrade: Ubuntu官方推荐的系…

代理缓存服务器squid

链接 &#xff1a;【Linux通过squid配置代理上网】 https://help.aliyun.com/knowledge_detail/41342.html?spm5176.7841507.2.4.nSXBP7 squid : 高性能的代理缓存服务器 Squid 是一个缓存 Internet 数据的软件&#xff0c;其接收用户的下载申请&#xff0c;并自动处理所下载的…

51单片机之音乐代码

世上只有妈妈好音乐源代码。 #include <reg51.h> sbit speaker P3^7;unsigned char timer0h, timer0l, time;//--------------------------------------//单片机晶振采用11.0592MHz// 频率-半周期数据表 高八位 本软件共保存了四个八度的28个频率数据code unsigne…

Python快速学习03:运算 缩进和选择

前言 系列文章&#xff1a;[传送门] 这篇昨晚本来要出的&#xff0c;去搭了帐篷&#xff0c;在学校的屋顶上。 运算 运算&#xff0c;不得不说的是运算符。 数学 , -, *, /, **, %,// 判断 , !, >, >, <, <, in 逻辑 and, or, not 数学运算符 例子 print (19) …

可以输入也可以下拉选择的select

https://www.cnblogs.com/leohui/p/654d.html 可以输入也可以下拉选择的select 我们知道&#xff0c;一般select下拉框是只能选择的&#xff0c;不能用来输入内容的。而有时我们会遇到下拉框中没有要选择的信息项或者下拉选项特别多时&#xff0c;我们可以让select变成text&am…

css-样式重构-代码分享

1.css reset /* reset 重置样式&#xff0c;清除浏览器默认样式&#xff0c;并配置适合设计的基础样式&#xff08;强调文本是否大多是粗体、主文字色&#xff0c;主链接色&#xff0c;主字体等&#xff09;。*/ html,body,h1,h2,h3,h4,h5,h6,div,dl,dt,dd,ul,ol,li,p,blockquo…

冯洛伊曼体系结构

布尔代数 是一种关于0 和 1 的代数系统&#xff0c;用基础的逻辑符号系统描叙物体和概念&#xff0c;是现代电子计算机的数学和逻辑基础 布尔量&#xff1a; 0 1   True, False 与&#xff1a; a, b ab a*b and 或&#xff1a; ab …

技术分类目录

https://www.helloweba.net/nav.html

Codeforces Round #324 (Div. 2) B. Kolya and Tanya 快速幂

B. Kolya and Tanya Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/problem/BDescription Kolya loves putting gnomes at the circle table and giving them coins, and Tanya loves studying triplets of gnomes, sitting in the ve…

Android 自定义Application

在android中 自定义Application 常用的作用是1 保存在程序运行中的全局变量实例&#xff1a;public class GlobalApp extends Application{ private UserData udata ; public UserData getudata(){ return udata; } public void setudat…

web基础,用html元素制作web页面

观察常用网页的HTML元素&#xff0c;在实际的应用场景中&#xff0c;用已学的标签模仿制作。 用div,form制作登录页面&#xff0c;尽可能做得漂亮。 练习使用下拉列表选择框&#xff0c;无序列表&#xff0c;有序列表&#xff0c;定义列表。 <!DOCTYPE html> <html la…

js遍历Object所有属性

https://www.cnblogs.com/itrena/p/9085009.html 在js中经常需要知道Object中的所有属性及值&#xff0c;然而若是直接弹出Object&#xff0c;则是直接显示一个对象&#xff0c;它的属性和值没有显示出来&#xff0c; 不是我们想要的结果&#xff0c;从而需要遍历Object的所有…

一,彻底理解第一个C语言程序 Hello World

对于初学者来说&#xff0c;第一个程序一般都是hello world&#xff0c;而且是照着书上一点一点敲的。所以&#xff0c;在初学者眼中&#xff0c;敲出来的第一个程序代码不过是一堆看不懂的英语。而事实上&#xff0c;C语言作为一门语言&#xff0c;是有语法的。所以这一节&…

es6的箭头函数

1.使用语法 : 参数 > 函数语句; 分为以下几种形式 :  (1) &#xff08;&#xff09;&#xff1d;&#xff1e;语句 ( )&#xff1d;&#xff1e; statement 这是一种简写方法省略了花括号和return 相当于 &#xff08;&#xff09;&#xff1d;&#xff1e;&#xff5…

三级分类菜单的数据库设计

http://www.imooc.com/article/285246?block_idtuijian_wz 最近在设计一款进销存系统的时候&#xff0c;遇到一个分类的设计问题&#xff0c;就是如何将分类设计成数据库里的表&#xff0c;怎么样设计才比较灵活&#xff1f; 举个例子&#xff0c;一级分类&#xff1a;生鲜类&…

双向环形链表

#ifndef DULIST_H #define DULIST_H /* 线性表的双向链表存储结构 */ typedef void * elemtype; typedef struct dulnode { elemtype data;struct dulnode *prior,*next; }dulnode,*dulinklist; /*带头结点的双向循环链表的基本操作(14个) */ void list_init(dulinklist *l)…

linux服务器加入windows域时报错Ticket expired

[rootrusky]# net ads join -U administrator Enter administrators password: kinit succeeded but ads_sasl_spnego_krb5_bind failed: Ticket expired Failed to join domain: failed to connect to AD: Ticket expired使用data命令查看时间&#xff0c;发现linux机器与wind…

机器学习问题解答

1、如何从字面正确理解weight decay的含义&#xff1f;它为何相当于L2范式正则化&#xff1f; 2、L2范数正则化对应贝叶斯统计里的哪个概念&#xff1f; 解答&#xff1a;http://blog.csdn.net/zouxy09/article/details/24971995/&#xff1b;http://t.hengwei.me/post/%E6%B5%…

搜索和数据分析引擎

https://www.elastic.co/cn/products/elasticsearch