题目描述

FJ is about to take his N (1 ≤ N ≤ 30,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.

每次只能从两边取,要求取出来之后字典序最小

输入输出格式

输入格式:

* 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

输出格式:

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.

输入输出样例

输入样例#1: 复制

6
A
C
D
B
C
B

输出样例#1: 复制

ABCBCD
开始的时候以为是个逗比贪心左边右边选最小的后来发现我逗比了。。。然后就开始脑补SA,对于一个字符串我们把它的后缀数组和前缀数组找出来慢慢比较。但是这样需要写两个SA,而且两个后缀数组比rank也不好比于是去%了一下学长,发现原来可以把字符串翻转后复制到字符串前面这样利用一个SA就可以解决了比的话直接比rank注意这里题目有误,每输出80个字符需要输出一个换行!!!!(害的我多调了半个小时。)

#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std;
const int MAXN=1e6+10;
int N,M;
int sa[MAXN],tax[MAXN],tp[MAXN],rak[MAXN],a[MAXN];
void Qsort()
{for(int i=0;i<=M;i++) tax[i]=0;for(int i=1;i<=N;i++) tax[rak[i]]++;for(int i=1;i<=M;i++) tax[i]+=tax[i-1];for(int i=N;i>=1;i--) sa[ tax[rak[tp[i]]]-- ] = tp[i];
}
void Ssort()
{M=450;N=N<<1|1;for(int i=1;i<=N;i++) rak[i]=a[i],tp[i]=i;Qsort();for(int w=1,p=0;p<N;M=p,w<<=1){    p=0;for(int i=N-w+1;i<=N;i++) tp[++p]=i;for(int i=1;i<=N;i++) if(sa[i]>w) tp[++p]=sa[i]-w;Qsort();swap(tp,rak);rak[sa[1]]=p=1;for(int i=2;i<=N;i++) rak[sa[i]]=(tp[sa[i]]==tp[sa[i-1]]&&tp[sa[i]+w]==tp[sa[i-1]+w])?p:++p;    }//for(int i=1;i<=N;i++) //    printf("%d ",rak[i]);int l=1,r=N/2+2,tot=0;while(tot<N/2) {tot++,rak[l]<rak[r]?printf("%c",a[l++]+'A'-1):printf("%c",a[r++]+'A'-1);if(tot%80==0) printf("\n");}}
int main()
{#ifdef WIN32freopen("a.in","r",stdin);freopen("a.out","w",stdout);#else#endifchar c;scanf("%d",&N);a[N+1]='*';for(int i=1;i<=N;i++){c='*';while(c<'A'||c>'Z') c=getchar();a[N+i+1]=c-'A'+1;a[N-i+1]=c-'A'+1;}Ssort();return 0;
}


转载于:https://www.cnblogs.com/zwfymqz/p/8413108.html

洛谷P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold相关推荐

  1. P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold(加强版)(贪心+hash哈希)

    P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold(加强版)(贪心+hash哈希) 洛谷上这道水题丧心病狂地把数据范围加到了500000 普通的做法肯定A不了了, ...

  2. 洛谷 P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…

    P1569 [USACO11FEB]属牛的抗议Generic Cow Prote- 题目描述 Farmer John's N (1 <= N <= 100,000) cows are li ...

  3. 洛谷P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…

    题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row and numbered 1..N. The cow ...

  4. 信息学奥赛一本通 1343:【例4-2】牛的旅行 | 洛谷 P1522 [USACO2.4] 牛的旅行 Cow Tours

    [题目链接] ybt 1343:[例4-2]牛的旅行 洛谷 P1522 [USACO2.4] 牛的旅行 Cow Tours [题目考点] 1. 图论 最短路径 Floyd算法 Floyd算法时间复杂度 ...

  5. 洛谷——P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

  6. 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  7. 洛谷P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  8. 洛谷P1821 [USACO07FEB]银牛派对Silver Cow Party

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  9. 洛谷——P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  10. 洛谷 P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

最新文章

  1. react绑定this_React绑定模式:处理“ this”的5种方法
  2. 利用Swoole同时更新多台服务器代码
  3. Elasticsearch搜索类型讲解(QUERY_THEN_FETCH,QUERY_AND_FEATCH,DFS_QUERY_THEN_FEATCH和DFS_QUERY_AND_FEATCH)...
  4. python教程是什么-Python基础教程_Python入门知识
  5. LOJ - #117. 有源汇有上下界最小流(有源汇有上下界的最小流)
  6. C++之map插入数据相同的key不能覆盖value解决办法
  7. 数据结构和算法———P3 时间复杂度和空间复杂度
  8. 9月29日云栖精选夜读:武装到“牙齿”!阿里云发布史上最强企业云安全架构 11层防护...
  9. .dll与.lib文件的区别
  10. 计算色光叠加的RGB颜色的alpha值
  11. 为底层元素注册监听器
  12. 混淆矩阵简介与Python实现
  13. css 都有哪些字体,css字体有哪些
  14. [css]画圆形标签
  15. 手机内存卡照片删了怎么恢复
  16. jsp全是问号_JSP response,request中文乱码(出现问号)总结
  17. 腾讯滑块vData_VMP分析
  18. 网关——Zuul学习
  19. 最简单的视频网站(JavaEE+FFmpeg)
  20. 11.18C语言练习题

热门文章

  1. Eclipse正式代替Oracle接管Java EE
  2. 大选期间完成25亿推送:美国移动push平台Urban Airship架构解密
  3. 这个锅,运维来背?忘记续期 HTTPS 证书,网易邮箱大量用户无法使用
  4. 数据中心已死:2025 年 80% 将关闭,目前 10%
  5. 如何正确使用开源项目?
  6. 标准的软件开发是怎样的?
  7. gulp怎么运行html文件,如果gulp-watch监视html文件,它会运行所有任务
  8. 网络攻防第七周作业之20189304李小涛
  9. Linux - 修改系统的max open files、max user processes (附ulimit的使用方法)
  10. JavaScript 火的有点过头了,但又能火多久呢?