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

本文介绍了一种算法,该算法能在限定的操作下(仅允许从序列两端取元素),找到使得序列字典序最小的排列方式。适用于如农场竞赛等需要进行字典序排序的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值