Best Cow Line
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 32687   Accepted: 8660

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

题意:字符串头和尾取出来,组成字典序最小的字符串
题解:贪心处理,倒序比较
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=; int N;
char a[]; int ans=; void solve(int k)
{
ans=;
int p=,q=k-;
while(q>=p)
{
int left=;
for(int i=;i+p<=q;i++)
{
if(a[p+i]<a[q-i])
{
left=;
break;
}
else if(a[p+i]>a[q-i])
{
left=;
break;
}
}
if(left)
{
printf("%c",a[p++]);
ans++;
}
else
{
printf("%c",a[q--]);
ans++;
} if(ans%==)
cout<<endl;
}
printf("\n");
}
int main()
{ cin>>N;
getchar();
for(int i=;i<N;i++)
{
cin>>a[i];
} solve(N);
}

poj3617 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 贪心模拟

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42701   Accepted: 10911 D ...

  3. poj3617 Best Cow Line(贪心,字典序问题)

    https://vjudge.net/problem/POJ-3617 这类字符串处理字典序问题经常用到贪心, 每决定输出一个字符之前,都要前后i++,j--逐个比大小,直至比出为止. #includ ...

  4. POJ3617 Best Cow Line【贪心】

    Description  给定长度为n的字符串S,要构造一个长度为n的字符串T.起初,T是空串,随后反复进行下列任意操作:  1.从S的头部删除一个字符,加到T的尾部  2.从S的尾部删除一个字符,加 ...

  5. bzoj4278[ONTAK2015]Tasowanie & bzoj1692[USACO 2007Dec]队列变换(Best Cow Line) 贪心正确性证明

    做法网上到处都有就不说了. 这题其实是之前做的….不过由于人太傻现在才想明白比较字典序进行贪心的正确性…. 方便起见,在两个串的最右端都加上很大但不相同的字符,避免第lcp+1个字符不存在的边界. 如 ...

  6. POJ 3617 Best Cow Line (贪心)

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

  7. POJ3617 Best Cow Line

    其实是学习参考了算法书的代码,但仍然是我自己写的,有小差别.贪心类型. #include <iostream> using namespace std; int main() { int ...

  8. POJ3617 Best Cow Line 馋

    虽然这个问题很简单,但非常好,由于过程是很不错的.发展思路的比较 并鼓励人们,不像有些贪心太偏,推动穷人,但恼人 鉴于长N弦S,然后又空字符串STR.每当有两个选择 1:删S增加虚假的第一要素STR于 ...

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

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

随机推荐

  1. SpringBoot---Web开发---WebSocket

    [广播式] 1. <?xml version="1.0" encoding="UTF-8"?> <project xmlns="ht ...

  2. code review的意义

    https://blog.csdn.net/brodycai/article/details/19636621

  3. Android 4.4及以后将内容布局延伸到状态栏

    首先说明:该文章不是大家说的沉浸式状态栏,网上沉浸式状态栏的博客很多,搜索就有了! 该篇博客的主要目的就是为了将图片显示在状态栏上,让APP看起来更有型!如下图所示:   界面 这个界面的布局就是co ...

  4. UI设计中蕴涵着系统重要的数据结构与功能设计

    UI设计中蕴涵着系统重要的数据结构与功能设计 UI设计中的用户需求,事件(用例)驱动

  5. hdfs校验和

    hdfs完整性:用户希望储存和处理数据的时候,不会有任何损失或者损坏.所以提供了两种校验: 1.校验和(常用循环冗余校验CRC-32). 2.运行后台进程来检测数据块. 校验和: a.写入数据节点验证 ...

  6. APP自动化测试

    CTS工具,主要是基于Androidinstrumentation和JUnit测试原理推单元测试用例: Monkey用来对UI进行压力测试,伪随机的模拟用户的按键输入,触摸屏输入,手势输入等: ASE ...

  7. github上fork原项目,如何将本地仓库代码更新到最新版本?

    场景: 在github上fork原项目,项目组成员发起pull request提交了代码,这时自己在本地仓库该如何更新到最新代码? 操作方法如下: 方法一.从github上进行操作然后更新 登录自己的 ...

  8. UVA 624 CD(01背包,要记录路径)

    题意: 有n张CD(n<=20),每张能播放的时长不同.给定一个时长限制t,挑出部分的CD使得总播放时间最长.顺便输出路径! 思路: 重点在输出路径,否则这题很普通.那就要用二维数组记录每个CD ...

  9. HDU - 5493 Queue 2015 ACM/ICPC Asia Regional Hefei Online(线段树)

    按身高排序,每个人前面最高的人数有上限,如果超出上限说明impossible, 每次考虑最小的人,把他放在在当前的从左往右第k+1个空位 因为要求字典序最小,所以每次k和(上限-k)取min值. 没有 ...

  10. Android(java)学习笔记108:Android的Junit调试

    1. Android的Junit调试: 编写android应用的时候,往往我们需要编写一些业务逻辑实现类,但是我们可能不能明确这个业务逻辑是否可以成功实现,特别是逻辑代码体十分巨大的时候,我们不可能一 ...