【贪心】POJ 3617 Best Cow Line

Best Cow Line
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 27986 Accepted: 7517

Description

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.

Sample Input

6
A
C
D
B
C
B

Sample Output

ABCBCD

Source


题意不难,给出一个长度为N的字符串,然后让你重新根据要求组合字符串并产生一个新的字符串,要求新的字符串字典序最小。
操作有两种:
1、将原先字符串的头部删除一个字符,增加在新的字符串的尾部
2、将原先字符串的尾部删除一个字符,增加在新的字符串的尾部
反复操作,直到原先的字符串全部字符操作完成。

模拟整个过程就好,对于原字符串,分别观察正序和逆序,永远选择字典序较小的方向取首字母就可以构造出字典序最小的新字符串,即比较目前操作的字符串的正序的字典序小还是逆序的字典序小,正序字典序小使用操作1,逆序字典序小使用操作2,相同两者任意皆可。
注意输入与输出的要求,,在这里PE了一发。

附上AC代码

#include<algorithm>
#include<stdio.h>
using namespace std;
void solve(void)
{
    int n;
    int cnt = 0;
    while(~scanf("%d",&n)){
        getchar();
        char a[n];
        for(int i = 0 ; i < n ; i++){
            scanf("%c",&a[i]);
            getchar();
        }
        int left = 0,right = n-1;
        while(left<=right){
            bool leftb = false;
            //模拟字符串字典序的比较。
            for(int i = 0 ; left+i <= right ; i++){
                if(a[left+i]<a[right-i]){
                    leftb = true;
                    break;
                }
                else if(a[left+i]>a[right-i]){
                    break;
                }
                //若左右相等就i++,继续比较下一字符的字典序
            }
            if(leftb) putchar(a[left++]);//从左边看字典序小于右边
            else putchar(a[right--]);
            cnt++;
            if(cnt%80==0){
                printf("\n");
                cnt=0;
            }
        }
        printf("\n");
    }
}

int main(void)
{
    solve();

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

两米长弦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值