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 贪心模拟的更多相关文章

  1. POJ 3617 Best Cow Line 贪心算法

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

  2. 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 ...

  3. POJ 3617 Best Cow Line (贪心)

    题意:给定一行字符串,让你把它变成字典序最短,方法只有两种,要么从头部拿一个字符,要么从尾部拿一个. 析:贪心,从两边拿时,哪个小先拿哪个,如果一样,接着往下比较,要么比到字符不一样,要么比完,也就是 ...

  4. POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心

    带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...

  5. POJ 3617 Best Cow Line(最佳奶牛队伍)

    POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...

  6. POJ 3617 Best Cow Line (贪心)

    Best Cow Line   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted: 4 ...

  7. poj 3617 Best Cow Line (字符串反转贪心算法)

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9284   Accepted: 2826 Des ...

  8. POJ 3617 Best Cow Line (字典序最小问题 & 贪心)

    原题链接:http://poj.org/problem?id=3617 问题梗概:给定长度为 的字符串 , 要构造一个长度为 的字符串 .起初, 是一个空串,随后反复进行下列任意操作. 从 的头部删除 ...

  9. poj 3617 Best Cow Line

    http://poj.org/problem;jsessionid=F0726AFA441F19BA381A2C946BA81F07?id=3617 Description FJ is about t ...

随机推荐

  1. leetcode: 0204 完成的

    目录 大纲:0204 完成的 notes ✅1051 高度检查器 ✅ 728 自除数 brute c解答: java switch 语句 java api: array 直接有 length 属性 , ...

  2. idea使用小技巧

    1.按住alt,鼠标往下拉一条直线,可以选中一列或多列,或者不选中任何文字,可以让光标定位到这几行的相同的列的位置,然后输入文本,发现在被选中的所有行同时输入了这些文本(类似notepad++): 2 ...

  3. Python - 同时运行两个以上的脚本

    在c.py中 import os os.system("python a.py") os.system("python b.py")

  4. 0X01应用程序黑客技术

    前言 该文章主要是讲解了常见的应用程序黑客技术基本概念,包括消息钩取,API钩取,DLL注入,代码注入 天象独行 0X01:消息钩取 原理:在我们通过键盘,鼠标等输入信息过程中,Windows会通过钩 ...

  5. Plastic Bottle Manufacturer - What Is The Direction Of Plastic Bottles?

    Plastic bottle manufacturers explain: protective packaging for cosmetic cleaning products requires b ...

  6. Unity热更新对比

    https://www.jianshu.com/p/f9d90edf4a7c Unity 热更新为啥用Lua 详解 ILRuntime的优势 同市面上的其他热更方案相比,ILRuntime主要有以下优 ...

  7. teraterm中状态框statusbox

    ;Author : Bing ;Date : 1/17/2019;Usage: modify log drictory according to actual drictorylogfile=&quo ...

  8. Mybatis plus 插入数据时将自动递增的主键手动进行赋值设置

    1.首先设置好实体类:将类型设置为 @TableId(type = IdType.INPUT) 2.在插入数据前将id赋值给实体类对象即可

  9. SSH项目Dao层和Service层及Action的重用

    泛型 1.泛型的定义 1.泛型是一种类型    1.关于Type            //是一个标示接口,该标示接口描述的意义是代表所有的类型        public interface Typ ...

  10. Python流程控制-2 条件判断

    条件判断 条件判断是通过一条或多条判断语句的执行结果(True或者False)来决定执行的代码块. 在Python语法中,使用if.elif和else三个关键字来进行条件判断. if语句的一般形式如下 ...