• Best cow Line(POJ 3617)


    题目链接:http://poj.org/problem?id=3617

    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
    题意:给定长度为N的字符串S,要构造一个长度为N的字符串T串。
    从S的头部删除一个字符,加到T的尾部
    从S的尾部删除一个字符,加到T的尾部
    目标是构造字典序尽可能小的字符串。
    解题思路:刚开始看到这题就想用两个字符数组,通过比较S的开头和末尾的大小中较小的放入T的末尾,当两个相等的时候就没细想,就随便选一个,然后码完发现连样例都通过不了,发现自己还是太差了,那么关键的情况竟然没有细想。这里我们采用贪心的思想,如果末尾和开头相同,我们则要比较下一个字符的大小,下一个字符也有可能相同
    附上代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 int s[2005];
     4 int main()
     5 {
     6     int n;
     7     while(scanf("%d",&n)!=EOF)
     8     {
     9         getchar();
    10         for(int i=0;i<n;i++)
    11         {
    12             scanf("%c",&s[i]);
    13             getchar();
    14         }
    15         int a=0,b=n-1;
    16         int ans=0;
    17         while(a<=b)
    18         {
    19             bool left=false;
    20             for(int i=0;a+i<=b;i++)
    21             {
    22                 if(s[a+i]>s[b-i])
    23                 {
    24                     left=false;
    25                     break;
    26                 }
    27                 else if(s[a+i]<s[b-i])
    28                 {
    29                     left=true;
    30                     break;
    31                 }
    32             }
    33             if(left)
    34             {
    35                 putchar(s[a]);
    36                 a++;
    37             }
    38             else
    39             {
    40                 putchar(s[b]);
    41                 b--;
    42             }
    43             ans++;
    44             if(ans==80)
    45             {
    46                 printf("
    ");
    47                 ans=0;
    48             }
    49         }
    50         printf("
    ");
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    015_stdc_C语言
    014_stdc_C语言
    013_stdc_C语言
    012C语言
    011_stdc_C语言
    010_stdc_C语言
    009_stdc_C语言
    008_stdc_C语言
    40.委托
    39.多线程
  • 原文地址:https://www.cnblogs.com/zjl192628928/p/9298950.html
Copyright © 2020-2023  润新知