码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-08-29 12:43:45      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

Best Cow Line, Gold
Time Limit: 5000MS   Memory Limit: 65536K
Total 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 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

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");
	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:

原文地址:http://blog.csdn.net/yu_ch_sh/article/details/48085669

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!