zoukankan      html  css  js  c++  java
  • 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.

    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的字符串S,还要利用一个空字符串tmp,可以进行任意一种操作:1.从S的头部删除一个字符加到tmp的尾部。2.从S的尾部删除一个字符加到tmp的尾部。
     
    目标是构造出按照字典序尽可能小的字符串tmp。
     
    要注意的是题目对输出还有别的要求: Every line (except perhaps the last one) contains the initials of 80 cows.
     
    首先想到的方法就是设置两个指针分别指向S的头部和尾部,然后比较,把较小的字符添加到tmp,但是这样是不一定对的,比如当头和尾一样大的时候就没法判断了。所以先得到S的逆序字符串reverse_S,然后每次比较S和reverse_S的头部字符,把较小的添加到tmp即可,类似于贪心。
     
    Source Code:
    #include <iostream>
    #include <cstring>
    #include <vector>
    
    using namespace std;
    
    char Cows_A[2010],Cows_B[2010];
    vector<char> answer;
    
    int main()
    {
        int N;
        int index_A,index_B;
        while(cin>>N){
            for(int i=0;i<N;++i)
                cin>>Cows_A[i];
            for(int i=0;i<N;++i)
                Cows_B[i]=Cows_A[N-i-1];
            answer.clear();
            index_A=index_B=0;
            while(answer.size()<(unsigned int)N){
                if(strcmp(Cows_A+index_A,Cows_B+index_B)<=0){
                    answer.push_back(*(Cows_A+index_A));
                    ++index_A;
                }else{
                    answer.push_back(*(Cows_B+index_B));
                    ++index_B;
                }
            }
            vector<char>::iterator iter;
            int cowsCount=0;
            for(iter=answer.begin();iter!=answer.end();++iter){
                cout<<*iter;
                ++cowsCount;
                if(cowsCount==80){
                    cout<<endl;
                    cowsCount=0;
                }
            }
            cout<<endl;
        }
        return 0;
    }
     
  • 相关阅读:
    Microsoft Web Camp
    [程序员学英语]26个英文字母
    原来接口是这样用的!一个例子搞定接口
    BizTalk Server 2010 培训
    [PM Tools]软件项目进度跟踪表v4.0
    BizTalk 开发系列(四十一) BizTalk 2010 BAM 安装手记
    WCF服务编程HelloWorld
    BizTalk 开发系列(三十九) BizTalk Server 2009技术概览
    WCF服务编程WCF应用程序的消息跟踪
    WCF服务编程基础
  • 原文地址:https://www.cnblogs.com/Murcielago/p/4221166.html
Copyright © 2011-2022 走看看