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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】「建议收藏」

    八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】「建议收藏」获取全套设计资源,请见后文说明…设计要求1)抢答器同时供8名选手或2个代表队比赛,分别用8个按钮S0-S7表示;2)设置一个系统清除和抢答控制开关S,该开关由主持人控制;3)抢答器具有锁存与显示功能。即选手按动按钮,锁存相应的编号,并在优先抢答选手的编号一直保持到主持人将系统清除为止;4)抢答器具有定时抢答功能,且一次抢答的时间由主持人设定(如30s等)。当主持人启动“开始”按键后,定时…

    2022年9月2日
    2
  • Python 源码混淆与加密

    Python 源码混淆与加密Python是一种解释型语言,没有编译过程,发布程序的同时就相当于公开了源码,这也是其作为开源语言的一个特性。但在某些场景下,我们的源码是不想被别人看到的,例如开发商业软件、编写0day漏洞POC/EXP、免杀shellcode等。多人学习python,不知道从何学起。很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手。很多已经做案例的人,却不知道如何去学习更加高深的知识。那么针对这三类人,我给大家提供一个好的学习平台,免费领取视频教程,电子书籍,以及课程的源代

    2022年8月23日
    13
  • SSM项目(GitHub上找的)

    SSM项目(GitHub上找的)SSM项目1.学生信息管理系统链接:https://pan.baidu.com/s/1e9ar4OKetL-40mp6R0b_4w提取码:01c8运行环境:jdk1.8以上服务器:tomcat运行软件:eclipse界面如下2.学生考试系统运行环境:jdk1.8以上服务器:tomcat运行软件:eclipse2.1学生前台2.2后台3.房屋出租系统运行环境:jdk1.8以上服务器:tomcat运行软件:eclipse…

    2022年6月29日
    44
  • 以太坊 如何挖矿_以太坊asic矿机

    以太坊 如何挖矿_以太坊asic矿机以太坊(ETH)是什么?它是公链之王,有人说它可能会超越比特币(BTC),其应用非常广泛,在以太坊世界里挖矿可以得到奖励,那么怎么挖矿?一下是以太坊的挖矿教程,相信看完教程后,你也能迅速的开始自己的挖矿之旅!我来详细道来。开始挖矿前的准备工作:1、硬件需求:系统要求.Windows7/8/10系统—–显卡要求.AMD或NVIDIA显卡,至少拥有4GB显存。2、软件准备:首先需要一款挖矿软件。Claymore’sDualMiner是原版挖矿软件需要掌握基础知识才可以使

    2022年10月15日
    0
  • Flask 的 jsonify解析

    Flask 的 jsonify解析首先运行如下代码:fromflaskimportFlask,jsonifyapp=Flask(__name__)tasks=[{‘id’:1,’title’:u’订阅python_mastery专栏’,’description’:u’专栏Link:https://xiaozhuanlan.com/python_mastery’},{‘id’:2,’t

    2022年5月24日
    34
  • Idea激活码最新教程2023.2.1版本,永久有效激活码,亲测可用,记得收藏

    Idea激活码最新教程2023.2.1版本,永久有效激活码,亲测可用,记得收藏Idea 激活码教程永久有效 2023 2 1 激活码教程 Windows 版永久激活 持续更新 Idea 激活码 2023 2 1 成功激活

    2025年5月26日
    1

发表回复

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

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