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

    • 原题如下:
      Best Cow Line
      Time Limit: 1000MS Memory Limit: 65536K
      Total Submissions: 32285 Accepted: 8560

      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
    • 思路:看完题目,很自然的可以想到贪心算法:不断取S的开头和结尾中较小的一个字符放到T的末尾,但对于相同的情形,我们还应该给出明确的选取方法,由于我们要尽快的将更小的字符加到新串T中,所以就要比较下一个字符,而下一个字符也可能相同,所以考虑将S反转之后和原串S进行比较,若S较小则将S开头的一个字符取出,加到新串T的结尾,否则将S结尾的一个字符取出,加到新串T的结尾
    • 代码:
       1 #include <cstdio>
       2 
       3 using namespace std;
       4 
       5 int n;
       6 char *s;
       7 int t;
       8 
       9 int main()
      10 {
      11     scanf("%d",&n);
      12     getchar();
      13     s = new char[n+1];
      14     for (int i=0; i<n; i++) 
      15     {
      16         s[i]=getchar();
      17         getchar();
      18     }
      19     int l=0, r=n-1;
      20     while (l<=r)
      21     {
      22         bool left=false;
      23         for (int i=0; l+i<=r; i++)
      24         {
      25             if (s[l+i]<s[r-i])
      26             {
      27                 left = true;
      28                 break;
      29             }
      30             else if (s[l+i]>s[r-i]) break;
      31         }
      32         if (left) 
      33         {
      34             ++t;
      35             putchar(s[l++]);
      36             if (t%80==0) printf("
      ");
      37         }
      38         else
      39         {
      40             ++t;
      41             putchar(s[r--]);
      42             if (t%80==0) printf("
      ");
      43         }
      44     }
      45 }
  • 相关阅读:
    将.lib库文件转换成.a库文件的工具
    协议
    协议
    bzoj1066
    bzoj2668
    bzoj2245
    bzoj2324后续思考
    bzoj2324
    jsoi2014前两轮回眸
    bzoj1293
  • 原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9416629.html
Copyright © 2011-2022 走看看