POJ3623:Best Cow Line, Gold(后缀数组)

POJ3623:Best Cow Line, Gold(后缀数组)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

Description

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.

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

Source

#include <iostream>#include <stdio.h>#include <string.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <bitset>#include <algorithm>#include <climits>using namespace std;#define LS 2*i#define RS 2*i+1#define UP(i,x,y) for(i=x;i<=y;i++)#define DOWN(i,x,y) for(i=x;i>=y;i--)#define MEM(a,x) memset(a,x,sizeof(a))#define W(a) while(a)#define gcd(a,b) __gcd(a,b)#define LL long long#define N 2000005#define MOD 1000000007#define INF 0x3f3f3f3f#define EXP 1e-8int wa[N],wb[N],wsf[N],wv[N],sa[N];int rank[N],height[N],s[N];//sa:字典序中排第i位的起始位置在str中第sa[i]//rank:就是str第i个位置的后缀是在字典序排第几//height:字典序排i和i-1的后缀的最长公共前缀int cmp(int *r,int a,int b,int k){    return r[a]==r[b]&&r[a+k]==r[b+k];}void getsa(int *r,int *sa,int n,int m)//n要包括末尾加入的0{    int i,j,p,*x=wa,*y=wb,*t;    for(i=0; i<m; i++)  wsf[i]=0;    for(i=0; i<n; i++)  wsf[x[i]=r[i]]++;    for(i=1; i<m; i++)  wsf[i]+=wsf[i-1];    for(i=n-1; i>=0; i--)  sa[--wsf[x[i]]]=i;    p=1;    j=1;    for(; p<n; j*=2,m=p)    {        for(p=0,i=n-j; i<n; i++)  y[p++]=i;        for(i=0; i<n; i++)  if(sa[i]>=j)  y[p++]=sa[i]-j;        for(i=0; i<n; i++)  wv[i]=x[y[i]];        for(i=0; i<m; i++)  wsf[i]=0;        for(i=0; i<n; i++)  wsf[wv[i]]++;        for(i=1; i<m; i++)  wsf[i]+=wsf[i-1];        for(i=n-1; i>=0; i--)  sa[--wsf[wv[i]]]=y[i];        t=x;        x=y;        y=t;        x[sa[0]]=0;        for(p=1,i=1; i<n; i++)            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)? p-1:p++;    }}void getheight(int *r,int n)//n不保存最后的0{    int i,j,k=0;    for(i=1; i<=n; i++)  rank[sa[i]]=i;    for(i=0; i<n; i++)    {        if(k)            k--;        else            k=0;        j=sa[rank[i]-1];        while(r[i+k]==r[j+k])            k++;        height[rank[i]]=k;    }}char str[10];int main(){    int n,i,j,k;    while(~scanf("%d",&n))    {        int len = 0;        for(i = 0; i<n; i++)        {            scanf("%s",str);            s[i] = str[0];            s[2*n-i] = str[0];        }        s[n] = 0;        s[2*n+2] = 0;        getsa(s,sa,2*n+2,200);        for(i = 0; i<2*n+2; i++)            rank[sa[i]] = i;        int l = 0,r = n+1;        while((len++)<n)        {            if(rank[l]<rank[r])            {                printf("%c",s[l]);                l++;            }            else            {                printf("%c",s[r]);                r++;            }            if(len%80==0)                printf("\n");        }        if(len%80)            printf("\n");    }    return 0;}

版权声明:本文博主原创文章。博客,未经同意不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116913.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ALLuxio_Alluxio公司怎么样

    ALLuxio_Alluxio公司怎么样一、什么是AlluxioAlluxio(之前名为Tachyon)是世界上第一个以内存为中心的虚拟的分布式存储系统。它统一了数据访问的方式,为上层计算框架和底层存储系统构建了桥梁。应用只需要连接Alluxio即可访问存储在底层任意存储系统中的数据。此外,Alluxio的以内存为中心的架构使得数据的访问速度能比现有常规方案快几个数量级。在大数据生态系统中,Alluxio介于计算框架(如Apache…

    2025年8月22日
    3
  • ansys随机振动分析_workbench扫频振动仿真

    ansys随机振动分析_workbench扫频振动仿真随机振动(PSD)分析步骤PSD分析包括如下六个步骤:1.建造模型;2.求得模态解;3.扩展模态;4.获得谱解;5.合并模态;6.观察结果。以上六步中,前两步跟单点响应谱分析一样,后四步将在下面作详细讲解。Ansys/Professional产品中不能进展随机振动分析。如果选用GUI交互方法进展分析,模态分析选择对话框〔MODOPT命令〕中包含有是否进展模态扩展选项〔MXPAND命令〕,将其设置为YES就可以进展下面的:扩展模态。这样,第二步〔求得模态解〕和第三步〔扩展模态〕就合并到一个步

    2022年10月10日
    2
  • 二叉树的中序遍历非递归算法java_二叉树遍历例题解析

    二叉树的中序遍历非递归算法java_二叉树遍历例题解析*非递归算法思想:  (1)设置一个栈S存放所经过的根结点(指针)信息;初始化S; (2)第一次访问到根结点并不访问,而是入栈;  (3)中序遍历它的左子树,左子树遍历结束后,第二次遇到根结点,就将根结点(指针)退栈,并且访问根结点;然后中序遍历它的右子树。 (4)当需要退栈时,如果栈为空则结束。     代码实现:void…

    2022年9月14日
    2
  • onedrive个人版免费扩容_onedrive扩容25t

    onedrive个人版免费扩容_onedrive扩容25tSkyDriveRenamedOneDriveSkyDriveProRenamed OneDriveforBusinessInvitefriendstojoinOneDriveForeachfriendwhosignsintoOneDriveasanewcustomer,bothyouandyourfriendwillreceiveanextra0.5

    2025年10月9日
    1
  • 激活函数-Sigmoid, Tanh及ReLU

    什么是激活函数在神经网络中,我们会对所有的输入进行加权求和,之后我们会在对结果施加一个函数,这个函数就是我们所说的激活函数。如下图所示。为什么使用激活函数我们使用激活函数并不是真的激活什么,这只是一个抽象概念,使用激活函数时为了让中间输出多样化,能够处理更复杂的问题。如果不适用结果函数的话,每一层最后输出的都是上一层输入的线性函数,不管加多少层神经网络,我们最后的输出也只…

    2022年4月3日
    66
  • java使用httpclient调用第三方接口

    java使用httpclient调用第三方接口java使用httpclient调用第三方接口HttpClientUtil工具类packagecom.fz.util;importjava.io.File;importjava.net.URL;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importorg.apache.ht…

    2022年5月24日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号