zoukankan      html  css  js  c++  java
  • Best Cow Line---POJ 3617(贪心)

    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

    *这题主要是用贪心,目标主要是从已知的字符串构造出字典序尽可能小的字符串

    *从字典序性质来看,无论字符串末尾有多大,只要保证前面部分较小就可以咯!

    *假设原来字符串为S,目标字符串为T,那么,不断取出S的开头和末尾较小的一个

    *字符放到T的末尾。上面的没有针对开头与结尾相同,如果,遇到这样动情况,应

    *该比较下一个字符的大小。如果,下一个字符也相同,那么可以得到下面的算法:

    *按照字典序比较S和将S反转后的字符串S’

    *如果,S较小,那么取出S开头字符,追加到T的末尾

    *如果,S'较小,那么取出S结尾字符,追加到T的末尾

    *如果,相同则取出那一个都可以。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 const int maxn =2010;
     7 int n;
     8 char s[maxn];
     9 
    10 int main(){
    11     cin>>n;
    12     for( int i=0; i<n; i++ ){
    13         cin>>s[i];
    14     }
    15     int a=0,b=n-1;
    16     int cnt=0;
    17     while(a<=b){
    18         bool left=false;
    19         for( int i=0; a+i<=b; i++ ){
    20             if(s[a+i]<s[b-i]){
    21                 left=true;
    22                 cnt++;
    23                 break;
    24             }
    25             else if(s[a+i]>s[b-i]){
    26                 left=false;
    27                 cnt++;
    28                 break;
    29             }
    30         }
    31         if(left) putchar(s[a++]);
    32         else putchar(s[b--]);
    33         if(cnt%80==0) putchar('
    ');
    34     }
    35     return 0;
    36 }
    有些目标看似很遥远,但只要付出足够多的努力,这一切总有可能实现!
  • 相关阅读:
    My Eclipse
    那一夜,我被梦中笑醒的事之(数据库)
    KTV项目总结
    欢迎来到,数据库联盟!
    学习手工创建表,表关系以及用exists 来查询
    sql 将Null 值转化成空字符串
    jquery toggle
    推荐一个不错的配色网站
    css之zindex
    关于前后端分离与不分离
  • 原文地址:https://www.cnblogs.com/Bravewtz/p/10612266.html
Copyright © 2011-2022 走看看