zoukankan      html  css  js  c++  java
  • poj 3617 Best Cow Line

    Best Cow Line
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 14718   Accepted: 4188

    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
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    char s[2001];
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            getchar();
            int cnt=0;
            for(int i=0;i<=n-1;i++)
                {
                    scanf("%c",&s[i]);
                    getchar();
                }
            int a=0,b=n-1;
            while(a<=b)
            {
             bool l=true;
            for(int i=0;a+i<=b;i++)
              {
                 if(s[a+i]<s[b-i])
                 {
                      break;
                 }
                 else
                    if(s[a+i]>s[b-i])
                 {
                      l=false;
                      break;
                 }
              }
               if(l)
                   {
                       printf("%c",s[a]);
                       a++;
                       cnt++;
                       if(cnt%80==0)
                          {
                              cout<<endl;
                              cnt=0;
                          }
                   }
               else
                 {
                     printf("%c",s[b]);
                     b--;cnt++;
                     if(cnt%80==0)
                         {
                            cout<<endl;
                            cnt=0;
                         }
                 }
            }
            if(cnt)
            printf("
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    javac 小记
    安全专家的工具箱
    MyBatis 缓存机制(十三)
    SpringMVC 环境搭建
    MyBatis 模糊查询的 4 种实现方式
    MyBatis 项目开发中是基于 XML 还是注解?
    MyBatis 动态 SQL 语句中出现 '<' 的问题
    数据库设计的三大范式
    mybatis 同时使用 XML 和注解
    数据库事务
  • 原文地址:https://www.cnblogs.com/smilesundream/p/6642557.html
Copyright © 2011-2022 走看看