Best Cow Line(贪心)

Best Cow Line(贪心)

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.

输入
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.

样例
input
6
A
C
D
B
C
B
output
ABCBCD

题意:给定一串字符,需要构造出一串新的字典序最小的字符,构造时每次只能从原字符串首尾选取一个字符作为新的字符串的元素。

题解:每次比较首尾两个字符的大小,选取其中较小的字符作为新的字符串的元素,如果遇到两个字符相等的情况就继续比较后面的值,直到它们不相等。

AC代码:

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <math.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    char a[2005],b[2005];
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
    }
    int l=0,r=n-1,p=0;          //分别用l,r来代表首尾的数组元素下标;
    while(p<n)                  //p代表新数组中的元素个数;
    {
        if(a[l]<a[r]&&p!=n-1)       //如果首元素小于尾元素,将首元素作为新的数组元素
        {                           //同时更新左侧下标值;
            b[p++]=a[l];
            l++;
        }
        else if(a[l]>a[r]&&p!=n-1)    //如果尾元素小于首元素,将尾元素作为新的数组元素
        {                             //同时更新右侧下标值;
            b[p++]=a[r];
            r--;
        }
        else if(a[l]==a[r]&&p!=n-1)   //如果首尾元素相等时,同时更新左右下标值进行比较;
        {
            int l2=l,r2=r;        //这里l2和r2主要是为了比较后面的值的大小                               
            while(a[l2]==a[r2])   //并不是用来更新下标的;
            {
                l2++;
                r2--;
            }
            if(a[l2]<a[r2])     //如果后面的值左侧小于右侧,更新左侧下标;
            {
                b[p++]=a[l];
                l++;
            }
            else
            {
                b[p++]=a[r];      //如果后面的值左侧大于右侧,更新右下标;
                r--;
            }
        }
        else if(p==n-1)            //这里注意的是最后一个字符没有比较对象,直接把它放在
        {                          //最后面,它的下标就是l的值,也是r的值;
            b[p++]=a[l];
        }
    }
    int countt=0;
    for(int i=0; i<p; i++)
    {
        cout<<b[i];
        countt++;
        if(countt==80)         //这里注意要每输出80个字符换一行;
        {cout<<endl;
        countt=0;
        }
    }
    printf("\n");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值