Best Cow Line(贪心)

FJ is about to take his N (1 ≤ N ≤ 2,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.
百度翻译:
福建即将带他的N(1≤N≤2000)头奶牛参加一年一度的“年度农民”比赛。在这场比赛中,每个农民都把牛排成一排,然后把它们赶过评委。

竞赛组织者今年采用了一种新的注册方案:只需按照每头奶牛的首字母出现的顺序注册(即,如果FJ按照这个顺序接收Bessie、Sylvia和Dora,他就注册了BSD)。在注册阶段结束后,根据奶牛名字首字母的字符串,对每个组进行递增字典顺序的判断。

FJ今年很忙,得赶紧回农场,所以他想尽早接受审判。他决定重新安排他的牛,谁已经排队,然后登记。

FJ标志着一个新的奶牛竞争线的位置。然后,他继续将母牛从旧线路编组到新线路,反复地将原线路(剩余部分)中的第一头或最后一头母牛发送到新线路的末端。当他完成后,FJ带着他的牛按这个新的顺序登记。

给定他的奶牛的初始顺序,确定他用这种方法能得到的最小的单词缩写字符串。

输入
*第1行:单个整数:N
*第2…N+1行:第i+1行包含原始行第i个位置的牛的单个首字母(‘a’…‘Z’)

输出
他所能做的最少的字典字串。每一行(可能最后一行除外)都包含新行中80头牛的首字母(‘A’…'Z’)。
Sample Input
6
A
C
D
B
C
B
Sample Output
ABCBCD
Sponsor

解析:
每次都是从队首和队尾选一个最小的,但如果他俩相等,那就要比较第二个和第n-1个,就这样一直比较相对称的两个字母,直到找到两个不一样的字母,标记小的那一端,最后输出,如果一直相等,那随便输出一个后还是下一个的寻找即可。
特别注意格式的控制,每80个就要换行。

代码:

#include"stdio.h"
char a[2020];
int main()
{
	int m;
	while(~scanf("%d",&m))
	{
		int i,x,y,s=0,f;
		for(i=0;i<m;i++)
		scanf("\n%c",&a[i]);
		x=0;
		y=m-1;
		while(x<=y)
		{
			f=0;
			for(i=0;x+i<=y;i++)
			{
				if(a[x+i]<a[y-i])
				{
					f=1;
					break;
				}
				else if(a[x+i]>a[y-i])
				{
					f=0;
					break;
				}
			}
			if(f)
			printf("%c",a[x++]);
			else
			printf("%c",a[y--]);
			s++;
			if(s==80)
			{
				printf("\n");
				s=0;
			}
		}
		printf("\n");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值