zoukankan      html  css  js  c++  java
  • 字典序最小问题 Best Cow Line (POJ 3617)

    Best Cow Line
    Time Limit: 1000MS        Memory Limit: 65536K
    Total Submissions: 32184        Accepted: 8533
    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

    先对第一个字母和最后一个字母进行比较,取较小的那个,如果相等对顺序序列和逆序序列进行比较,取小的那头。

    #include<iostream>
    #include<algorithm>
    #include<cstdlib>
    #include<cstdio>
    using namespace std;
    int main()
    {
        char cow[2010],ne[2010];
        int n;
        scanf("%d",&n);
        getchar();
        for(int i=0;i<n;i++){
            scanf("%c",&cow[i]);
            getchar();
        }
        int a=0,b=n-1,j=0;
        while(a<=b)
        {
            bool cmp=false;
            for(int i=0;a+i<=b;i++)
            {
                if(cow[a+i]<cow[b-i])
                {
                    cmp=false;
                    break;
                }
                else if(cow[a+i]>cow[b-i])
                {
                    cmp=true;
                    break;
                }
            }
            if(cmp)
            {
                ne[j++]=cow[b--];
            }
            else
            {
                ne[j++]=cow[a++];
            }
        }
        for(int i=0;i<n;i++){
            printf("%c",ne[i]);
            if((i+1)%80==0&&i<n-1)
                printf("
    ");
        }
    }
    
  • 相关阅读:
    HDOJ1269 迷宫城堡
    最长公共子序列 nyoj36
    HDU1081 To The Max 求子矩阵最大和
    nyoj20 吝啬的国度
    景观分析工具:arcgis中patch analysis模块
    景观格局动态变化分析方法(基于ArcGIS、Fragstats、ENVI、ERDAS、Patch Analysis for ArcGIS) (20110315 08:07:03)
    从C#到Python —— 谈谈我学习Python一周来的体会
    如何判定多边形是顺时针还是逆时针
    超新星与暗能量的发现--今年诺贝尔物理奖工作的介绍(转)
    怎样把扫描好的身份证打印出实际大小
  • 原文地址:https://www.cnblogs.com/ke-yi-/p/10175835.html
Copyright © 2011-2022 走看看