• 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;
    }
     
  • 相关阅读:
    使用jQuery对象:DOM属性、CSS、尺寸、遍历、DOM操作、事件、特效
    jQuery常见代码,注意事项
    使用jQuery函数:选择器 工具类 Ajax
    jQuery的两把利器:jQuery的核心函数 jQuery的核心对象 补充:伪数组
    初识jQuery:jQuery是什么,为什么要用它? 2种js库库文件 2种引用JS库的方式 使用jQuery jQuery内部简略结构
    H5 Web Workers:什么是 Web Worker? 使用 图解 Web Workers和DOM
    线程机制与事件机制:进程与线程 浏览器内核 定时器引发的思考 JS是单线程执行的 浏览器的事件循环(轮询)模型
    对象创建模式 继承模式 补充(new一个对象的背后做了些什么?)
    【React+antd】做一个自定义列的组件
    当你要给页面插入背景音乐
  • 原文地址:https://www.cnblogs.com/Murcielago/p/4221166.html
Copyright © 2020-2023  润新知