前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >洛谷P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold

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

作者头像
attack
发布2018-04-11 13:10:09
4710
发布2018-04-11 13:10:09
举报

题目描述

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

代码语言:javascript
复制
6
A
C
D
B
C
B

输出样例#1:

代码语言:javascript
复制
ABCBCD
代码语言:javascript
复制
开始的时候以为是个逗比贪心左边右边选最小的后来发现我逗比了。。。
然后就开始脑补SA,
对于一个字符串我们把它的后缀数组和前缀数组找出来慢慢比较。
但是这样需要写两个SA,而且两个后缀数组比rank也不好比
于是去%了一下学长,发现原来可以把字符串翻转后复制到字符串前面
这样利用一个SA就可以解决了
比的话直接比rank
注意这里题目有误,每输出80个字符需要输出一个换行!!!!(害的我多调了半个小时。)
代码语言:javascript
复制
#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;
}
代码语言:javascript
复制
代码语言:javascript
复制
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-02-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
  • 输入输出格式
  • 输入输出样例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档