洛谷P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold
题目描述
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<br /><br /><br /><br />
<strong>开始的时候以为是个逗比贪心左边右边选最小的后来发现我逗比了。。。</strong><br /><strong>然后就开始脑补SA,</strong><br /><strong>对于一个字符串我们把它的后缀数组和前缀数组找出来慢慢比较。</strong><br /><strong>但是这样需要写两个SA,而且两个后缀数组比rank也不好比</strong><br /><strong>于是去%了一下学长,发现原来可以把字符串翻转后复制到字符串前面</strong><br /><strong>这样利用一个SA就可以解决了<br />比的话直接比rank<br />注意这里题目有误,每输出80个字符需要输出一个换行!!!!(害的我多调了半个小时。)<br /><br /></strong>
#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; }
<br /><br /><br /><br />
<br /><br />