• POJ-3617-Best Cow Line


    http://poj.org/problem?id=3617

    Best Cow Line
    Time Limit: 1000MSMemory Limit: 65536K
    Total Submissions: 10603Accepted: 3187

    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和一个初始为空的字符串T,每次可以从S的头或者尾取一个字符加入T的后面,求最终字典序最小的T。那么可以贪心的取当前为最优的即可。

     PS.挑战程序设计竞赛第二版P43~P45就是讲解这道题目的

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 
     5 //定义数据规模
     6 #define MAXN 2020
     7 
     8 //输入
     9 int N;
    10 char str[MAXN];
    11 
    12 void solve(){
    13     //剩余的字符串为str[a], str[a + 1],...,str[b]
    14     int a = 0, b = N - 1, cnt = 0;//cnt用于统计已经输出的字符数
    15     while(a <= b){
    16         //将从左起和从右起的字符串比较
    17         bool left = false;
    18         for(int i = 0; a + i <= b; i++){
    19             if(str[a + i] < str[b - i]){
    20                 left = true;
    21                 break;
    22             }
    23             else if(str[a + i] > str[b - i]){
    24                 left = false;
    25                 break;
    26             }
    27         }
    28         if(left){
    29             putchar(str[a++]);
    30         }
    31         else{
    32             putchar(str[b--]);
    33         }
    34         cnt++;
    35         if((cnt) % 80 == 0){//如果输的字符数是80的倍数,则换行
    36             putchar(' ');
    37         }
    38     }
    39     if((cnt) % 80 != 0){//判断最后是否要换行
    40         putchar(' ');
    41     }
    42 }
    43 
    44 int main(){
    45     int i;
    46     while(scanf("%d", &N) != EOF){
    47         for(i = 0; i < N; i++){
    48             scanf(" %c", &str[i]);// 为了忽略读入的回车
    49         }
    50         solve();
    51     }
    52     return 0;

    53 } 

  • 相关阅读:
    C# 基于密钥的64位加密与解密方法(原创)
    爱情 前途 命运
    设计模式学习笔记装饰模式
    jquery暂无图片插件
    IIS GZIP压缩(转)
    Fckeditor使用笔记
    设计模式学习笔记策略模式
    电子商务网站搜索架构方案
    批量修改数据库表的架构sql
    win 2003 安装 vs2005 sp1 问题1718
  • 原文地址:https://www.cnblogs.com/angle-qqs/p/4081786.html
Copyright © 2020-2023  润新知