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,现在把这个S变成一个字符串T:每次从S的头或尾取一个字符添加到T里,要求其字典序最小。

思路:显然是贪心求解,如果S头不等于S尾,那么取小的一边加到T里。

但是如果相同,不能任取,如BCAB,如果任取B,可能变成了BABC或者BBAC,后者显然不合标准。

应该一直比较,知道不相同或者全部都相同:不相同就取小的那一边,全部相同就随意取。

比较的过程可以暴力,也可以后缀数组。

普通贪心代码:

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
char c[maxn];
bool Left(int L,int R){
while(L<=R){
if(c[L]!=c[R])
return c[L]<c[R];
L++; R--;
}
return true;
}
int main()
{
int N,cnt;
scanf("%d",&N);
for(int i=;i<=N;i++) cin>>c[i];
int L=,R=N; cnt=;
while(L<=R){
if(Left(L,R)) cout<<c[L++];
else cout<<c[R--];
cnt++;
if(cnt==) printf("\n"),cnt=;
}
return ;
}

后缀数组代码:

#include<cmath>
#include<math.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int N;char c[maxn];
struct SA
{
int Rank[maxn],A[maxn],B[maxn],cntA[maxn],cntB[maxn],sa[maxn],tsa[maxn],ht[maxn],Min[maxn][];
void sort(){
for(int i=;i<=;i++) cntA[i]=;
for(int i=;i<=N;i++) cntA[c[i]]++;
for(int i=;i<=;i++) cntA[i]+=cntA[i-];
for(int i=N;i>=;i--) sa[cntA[c[i]]--]=i;
Rank[sa[]]=;
for(int i=;i<=N;i++) Rank[sa[i]]=Rank[sa[i-]]+(c[sa[i]]==c[sa[i-]]?:);
for(int l=;Rank[sa[N]]<N;l<<=){
for(int i=;i<=N;i++) cntA[i]=cntB[i]=;
for(int i=;i<=N;i++) cntA[A[i]=Rank[i]]++;
for(int i=;i<=N;i++) cntB[B[i]=i+l<=N?Rank[i+l]:]++;
for(int i=;i<=N;i++) cntA[i]+=cntA[i-],cntB[i]+=cntB[i-];
for(int i=N;i>=;i--) tsa[cntB[B[i]]--]=i;
for(int i=N;i>=;i--) sa[cntA[A[tsa[i]]]--]=tsa[i];
Rank[sa[]]=;
for(int i=;i<=N;i++) Rank[sa[i]]=Rank[sa[i-]]+(A[sa[i]]==A[sa[i-]]&&B[sa[i]]==B[sa[i-]]?:);
}
}
}S;
int main()
{
int n,cnt;
scanf("%d",&n); N=n+n+;
for(int i=;i<=n;i++) cin>>c[i];
for(int i=n;i>=;i--) c[N-i]=c[i];
int L=,R=n; cnt=; S.sort();
while(L<=R){
if(S.Rank[L]<=S.Rank[N-R]) cout<<c[L++];
else cout<<c[R--];
cnt++;
if(cnt==) printf("\n"),cnt=;
}
return ;
}

POJ3617:Best Cow Line (贪心&&后缀数组)的更多相关文章

  1. poj 3623 Best Cow Line, Gold 后缀数组 + 贪心

    题目链接 题目描述 对于一个给定的字符串,可以从左右两端取字符,依次排列构成一个新的字符串. 求可能构成的字符串中字典序 最小的一个. 例:ACDBCB -> ABCBCD 思路 参考自 xue ...

  2. POJ3623:Best Cow Line, Gold(后缀数组)

    Description FJ is about to take his N (1 ≤ N ≤ 30,000) cows to the annual"Farmer of the Year&qu ...

  3. 【bzoj4278】[ONTAK2015]Tasowanie 贪心+后缀数组

    题目描述 给定两个数字串A和B,通过将A和B进行二路归并得到一个新的数字串T,请找到字典序最小的T. 输入 第一行包含一个正整数n(1<=n<=200000),表示A串的长度. 第二行包含 ...

  4. 【bzoj1692】[Usaco2007 Dec]队列变换 贪心+后缀数组

    题目描述 FJ打算带他的N(1 <= N <= 30,000)头奶牛去参加一年一度的“全美农场主大奖赛”.在这场比赛中,每个参赛者都必须让他的奶牛排成一列,然后领她们从裁判席前依次走过. ...

  5. poj3617 best cow line(贪心题)

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

  6. [bzoj1692][Usaco2007 Dec]队列变换——贪心+后缀数组

    Brief Description 给定一个数列,您每次可以把数列的最前面的数或最后面的数移动到新数列的开头,使得新数列字典序最小.输出这个新序列. Algorithm Design 首先我们可以使用 ...

  7. POJ 3617 Best Cow Line 贪心算法

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

  8. poj 3617 Best Cow Line 贪心模拟

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

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

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

随机推荐

  1. sql通配符+sql中查询条件包含下划线等通配符的写法

    一.SQL 通配符 在搜索数据库中的数据时,SQL 通配符可以替代一个或多个字符. SQL 通配符必须与 LIKE 运算符一起使用. 在 SQL 中,可使用以下通配符: 通配符 描述 % 替代一个或多 ...

  2. [HDU4348]To the moon(主席树)

    传送门 对于这个题,显然要打lazy标记了,但是lazy标记pushdown的时候肯定会增加一大堆节点,然后就MLE了.(题解这么说的,我其实不会pushdown) 所以,就换另一种方式,把标记直接打 ...

  3. poj 3678 XOR和OR和AND(简单2-sat问题)

    /* 题意:给你一些边,每条边有一个值和一个运算符XOR OR AND求是否存在一些点使得所有的边根据这些运算符 可以符合条件的权值. 建边方式参考:http://blog.csdn.net/shua ...

  4. 深入理解计算机操作系统——第11章:CS模型,网络

    网络编程: 11.1 客户端-服务器编程模型 (1)一个应用是由一个服务器进程和一个或多个客户端进程组成. (2)服务器管理某种资源,并且操纵这种资源来为客户端服务. CS模型: CS的基本操作是事务 ...

  5. python学习之-- logging模块

    logging模块功能:提供了标准的日志接口,可以通过它存储各种格式的日志.日志5个级别分:debug(),info(),warning(),error(),critical() logging.ba ...

  6. 2017CodeM复赛

    A.配对游戏(loj6191) 题目: https://loj.ac/problem/6191 分析: g[i][j]表示前i个位置尽可能合并,合并到最后右边剩下j个>,这样情况的概率 那么g[ ...

  7. Tomcat可以实现Session共享方案

    说明:原来Tomcat也是可以实现Session共享的,这样大大减少的硬编码的实现,并且前面用Nginx分流时不用考虑Session的问题,因为是Web容器提供了Session共享的支持. 1.在每个 ...

  8. hp 88a加粉

    http://v.youku.com/v_show/id_XNzEzODEwNzMy.html

  9. Java同步锁何时释放?

    在测试java多线程中有关 “生产者和消费者” 这个经典问题的时候,写代码测试的时候,思考到一些问题(所以还是要动手,实践才能储真知啊), synchronize 同步锁何时释放,何时获得?重新获得锁 ...

  10. Binder系列8—如何使用Binder(转)

    一.Native层Binder 源码结构: ClientDemo.cpp: 客户端程序 ServerDemo.cpp:服务端程序 IMyService.h:自定义的MyService服务的头文件 IM ...