POJ - 3617 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.

输入说明:

  • 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

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

SAMPLEOUTPUT:

ABCBCD

思路:

题意是告诉我们n头牛的字母编号,这些奶牛排成一列,我们要从中选出字典顺序最大的组合,每次选择都是从队伍头部,或者队伍尾部开始的。在选择的过程中要比较队首和队尾的字母谁大,谁大就取谁,但是也会有遇到两个字母相同的情况,那就要继续比较队首第二个和队尾倒数第二个的大小,如果相等再继续,知道全部遍历完或者出现不同的。每取一个直接数据即可,不需要再额外存储刀一个数组中。

AC代码:

#include<iostream>
using namespace std;
char s[2001];
int main()
{
    int n,a=0,b,ans,left,i;
    cin>>n;
    for(int i=0; i<n; i++)//半年前写的输入,现在看着好刺眼,好拙劣的输入方式。
    {
        cin>>s[i];
    }
    b=/**/n-1;
    ans=0;
    while(a<=b)
    {
        left=0;
        for(i=0;a+i<=b;i++)
        {
            if(s[a+i]<s[b-i])
            {
                left=1;
                break;
            }
            else if(s[a+i]>s[b-i])
            {
                left=0;
                break;
            }
        }
        if(left==1)
        {
            cout<<s[a++];
        }
        else
        {
            cout<<s[b--];
        }
        ans++;
        if(ans==80)
        {
            cout<<endl;
            ans=0;
        }
    }
    cout<<endl;
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值