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. python015 Python3 函数

    Python3 函数函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段.函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也可 ...

  2. 一个SAM的样例

    \[s=abcbacbcb\\ \begin{split} p \quad& fa \quad& Substrings \quad& Right \\ 1 \quad& ...

  3. 624. Maximum Distance in Arrays

    Problem statement Given m arrays, and each array is sorted in ascending order. Now you can pick up t ...

  4. spring几种依赖注入方式以及ref-local/bean,factory-bean,factory-method区别联系

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...

  5. 普通平衡树(bzoj 3224)

    Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...

  6. TYVJ P 1214 硬币问题

    TYVJ  P 1214 硬币问题 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述   有n种硬币,面值为别为a[1],a[2],a[3]……a[n],每种都 ...

  7. 【cmd】cmd常用命令

    dir 是英文单词directory(目录)的缩写,主要用来显示一个目录下的文件和子目录 md  是英文make directory(创建目录)的缩写 cd  是英文change directory( ...

  8. 【Tomcat】如何优化tomcat配置(从内存、并发、缓存4个方面)优化

    一.Tomcat内存优化 ** Tomcat内存优化主要是对 tomcat 启动参数优化,我们可以在 tomcat 的启动脚本 catalina.sh 中设置 java_OPTS 参数. JAVA_O ...

  9. Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings [dp 前缀和 ]

    传送门 D. A and B and Interesting Substrings time limit per test 2 seconds memory limit per test 256 me ...

  10. 从零开始写STL—模板元编程之any

    any class any; (since C++17) The class any describes a type-safe container for single values of any ...