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
  • 相关阅读:
    idea最新注册码
    pycharm中可以运行脚本(只在控制台运行,Debugger不运行,设置的断点没用)但是不能debug脚本
    VSCode 云同步扩展设置 Settings Sync 插件
    gist.github.com 无法访问解决办法,亲测永远有效!
    C# HttpWebRequest httpclient
    C# 图片处理
    powerdesigner逆向工程生成PDM时的列注释
    Ocelot网关治理
    Consul服务注册与发现
    CentOS 使用DVD1_DVD2作为本地离线的更新源
  • 原文地址:https://www.cnblogs.com/-maybe/p/4470518.html
Copyright © 2011-2022 走看看