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;
    }
     
  • 相关阅读:
    Android之文件&XML
    Dialog组件
    Android模拟器使用SD卡(2)
    Android 蓝牙开发浅析
    创建和解析XML
    android 读取文件相关
    android 网络编程 HttpGet类和HttpPost类使用详解
    Android模拟器使用SD卡(1)
    Android中控件的OnKeyListener()事件
    [转]Android蓝牙开发浅谈
  • 原文地址:https://www.cnblogs.com/Murcielago/p/4221166.html
Copyright © 2011-2022 走看看