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 ;
}

最新文章

  1. sql分页操作
  2. 初始JavaScript
  3. NOI 4976:硬币
  4. dreamwaver的动态相关文件 工具栏搜索
  5. NGUI 图片变灰
  6. Map 映射
  7. 09.C#委托转换和匿名方法(五章5.1-5.4)
  8. 解决JsonFormat日期少一天问题
  9. cocos2d-x anchorPoint
  10. Attribute(两)——定义自己的特色+Asp.net MVC中间filter详细解释
  11. PLSQL 保存布局
  12. GCD之死锁体会
  13. c#多线程同步之EventWaitHandle再次使用
  14. Docker安装ElasticSearch及kibana
  15. 开源GIS浅谈 【转】
  16. java实验四《Android程序设计》实验报告
  17. php SQL 防注入的一些经验
  18. C#中获取用户登录IP地址
  19. Leetcode 回文数字判断
  20. Solr4:配置Data Import,从数据库直接创建索引

热门文章

  1. Leetcode 300.最长上升子序列
  2. 转载:K-means聚类算法
  3. j_spring_security_check 404错误
  4. Can you answer these queries(spoj 1043)
  5. C++动态特性和C++对象模型——《高质量程序设计12章》
  6. MITM Proxy环境搭建
  7. Flex 4 自定义预加载器
  8. poj3635 FULL tank(TLE) 有限制的最短路(BFS搜索)。
  9. python学习之-项目开发目录规范
  10. 一份关于webpack2和模块打包的新手指南(一)