• 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
  • 相关阅读:
    Streams那些事之概述与原理
    ORA12514: ORACLE 监听错误
    使用软件工具插件 备忘
    jquery 弹出遮罩插件 prettyPhoto 参数说明
    SQL 9位随机码
    向上下左右不间断无缝滚动的效果(兼容火狐和IE)[转]
    SQL 将一串字符串转换为列插入临时表
    jQuery插件开发全解析[转]
    DIV+CSS命名规范
    ASP.NET中Session跨站点共享实现方式
  • 原文地址:https://www.cnblogs.com/-maybe/p/4470518.html
Copyright © 2020-2023  润新知