zoukankan      html  css  js  c++  java
  • POJ3617 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
     1 #include<iostream>
     2 #include<cstring>
     3 
     4 using namespace std;
     5 
     6 const int maxn=2005;
     7 
     8 char a[maxn];
     9 char ans[maxn];
    10 
    11 int main()
    12 {
    13     int n;
    14     while(cin>>n)
    15     {
    16         for(int i=1;i<=n;i++)
    17              cin>>a[i];
    18 
    19         int tot=1;
    20         int left=1;
    21         int right=n;
    22 
    23         while(left<=right)
    24         {
    25             int i=left;
    26             int j=right;
    27 
    28             while(a[i]==a[j])
    29             {
    30                 i++;
    31                 j--;
    32             }
    33             if(a[i]<a[j])
    34             {
    35                 ans[tot++]=a[left++];
    36             }
    37             else
    38             {
    39                 ans[tot++]=a[right--];
    40             }
    41         }
    42         
    43         for(int i=1;i<tot;i++)
    44         {
    45             cout<<ans[i];
    46             if(i%80==0)
    47                 cout<<endl;
    48         }
    49         
    50     }
    51 
    52     return 0;
    53 }
    View Code
  • 相关阅读:
    uestc1307 windy数 ——数位DP入门题
    2013年4月3日 小雨,阴
    hdu1202 The calculation of GPA ——水题
    zoj 3693 Happy Great BG
    hdu 2035 人见人爱A^B ——同余的简单性质
    zoj2913 Bus Pass ——BFS入门题
    一个bug,持续更新……
    zoj 3406 Another Very Easy Task
    poj 1995 Raising Modulo Numbers ——快速幂
    hdu 1059 Dividing ——多重背包复习
  • 原文地址:https://www.cnblogs.com/-maybe/p/4470518.html
Copyright © 2011-2022 走看看