洛谷P2870 [USACO07DEC]最佳牛线,黄金Best Cow …
2018-06-17 21:18:26来源:未知 阅读 ()
题目描述
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.
输入输出样例
6 A C D B C B
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 WIN32 freopen("a.in","r",stdin); freopen("a.out","w",stdout); #else #endif char 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; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 洛谷P1164->小A点菜 2020-05-18
- 洛谷P1907口算练习题 2020-03-24
- 结题报告--P5551洛谷--Chino的树学 2020-03-13
- 结题报告--洛谷P3915 2020-03-13
- 洛谷P1034 矩形覆盖 2020-03-10
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash