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


相关推荐

  • Eclipse断点调试

    Eclipse断点调试作为开发人员,掌握开发环境下的调试技巧十分有必要。去年就想把关于Eclipse断点调试总结下了,由于对时间的掌控程度仍需极大提高,结果拖到今年才写了此篇博文。关于java调试技术还有很多,如JavaDebugInterface等,依据具体项目的需要,还有很多值得去研究和学习的。该博文仅就Eclipse断点调试技巧做下总结,不足够的地方还请大牛们指点。1 Debug视图1.1线程堆栈

    2022年5月21日
    95
  • 为什么pycharm下载不了第三方库_pycharm详细使用教程

    为什么pycharm下载不了第三方库_pycharm详细使用教程单独

    2022年8月25日
    6
  • idea进入方法快捷键详情大全(idea快捷键大全最新设置)[通俗易懂]

    idea进入方法快捷键详情大全(idea快捷键大全最新设置)[通俗易懂]首页>软件应用>返回首页idea进入方法快捷键详情大全(idea快捷键大全最新设置)软件应用发布时间:2022-02-1311:05:09刚开始使用IDEA时一直都不熟悉,利用空闲的时间整理了一下常用的IDEA快捷键。1、Ctrl快捷键介绍Ctrl+F在当前文件进行文本查找Ctrl+R在当前文件进行文本替换Ctrl+Z撤销Ctrl+Y删除光标所在行或删除选中的行Ctrl+X剪切光标所在行或剪切选择内容…

    2022年10月2日
    4
  • UML工具到底哪个更好用

    UML工具到底哪个更好用经典如EnterpriseArchitect美丽如MagicDrawUML免费如StarUML容易上手visioRose商业PowerDesigner很早的UML建模工具用的广泛评价好,可以直接生成数据库脚本试试VS2010版,里面自带有UML开发工具EA文档生成RTF格式很好使,对UML的支持也比较好,正向/反向工程都还可以ROSE太大…

    2022年7月16日
    21
  • 高中688个高频单词_高频英语单词800

    高中688个高频单词_高频英语单词800给一非空的单词列表,返回前 k 个出现次数最多的单词。返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。示例 1:输入: [“i”, “love”, “leetcode”, “i”, “love”, “coding”], k = 2输出: [“i”, “love”]解析: “i” 和 “love” 为出现次数最多的两个单词,均为2次。 注意,按字母顺序 “i” 在 “love” 之前。 示例 2:输入: [“the”, “day”, “is

    2022年8月9日
    9
  • AfxMessageBox和MessageBox差别

    AfxMessageBox和MessageBox差别

    2021年11月30日
    52

发表回复

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

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