POJ 题目3623 Best Cow Line, Gold(后缀数组rank简单应用)

来源:互联网 发布:白象梳篦淘宝店铺 编辑:程序博客网 时间:2024/05/17 12:56
Best Cow Line, Gold
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 5063 Accepted: 1806

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 theith 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

6ACDBCB

Sample Output

ABCBCD

Source

USACO 2007 December Gold

题目大意:只能从两头取,使字典序最小

ac代码

Problem: 3623  User: kxh1995 Memory: 1364K  Time: 438MS Language: C++  Result: Accepted 

#include<stdio.h>                 #include<string.h>                 #include<algorithm> #include<stdlib.h>                #include<iostream>                #define min(a,b) (a>b?b:a)             #define max(a,b) (a>b?a:b)     #define INF 0xfffffff #define N 60060            using namespace std;  char str[N];int sa[N],t1[N],t2[N],c[N];int rank[N];void build_sa(char *s,int n,int m){int i,j,p,*x=t1,*y=t2;for(i=0;i<m;i++)c[i]=0;for(i=0;i<n;i++)c[x[i]=s[i]]++;for(i=1;i<m;i++)c[i]+=c[i-1];for(i=n-1;i>=0;i--)sa[--c[x[i]]]=i;for(j=1;j<=n;j<<=1){p=0;for(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<m;i++)c[i]=0;for(i=0;i<n;i++)c[x[y[i]]]++;for(i=1;i<m;i++)c[i]+=c[i-1];for(i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];//swap(x,y);int *T=x;x=y;y=T;p=1;x[sa[0]]=0;for(i=1;i<n;i++)x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++;if(p>=n)break;m=p;}}int main(){int n;while(scanf("%d",&n)!=EOF){char s[2];int i;for(i=0;i<n;i++){scanf("%s",s);str[i]=s[0];str[n*2-i]=s[0];}str[n]=0;str[2*n+1]=0;build_sa(str,2*n+2,200);for(i=0;i<2*n+2;i++)rank[sa[i]]=i;int l=0,r=n+1;int len=0;while((len++)<n){if(rank[l]<rank[r]){printf("%c",str[l]);l++;}else{printf("%c",str[r]);r++;}//len++;if(len%80==0)printf("\n");}if(len%80)printf("\n");}}


0 0
原创粉丝点击