• POJ 3617 Best Cow Line


    Best Cow Line
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 18526   Accepted: 5152

    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

    Source

     
     
     
    解析:贪心。题意为给定长度为N的字符串T,构造一个长度为N的字符串T,要求使字符串T的字典序尽可能小。起初,T是一个空串,随后反复进行下列任一操作:
    •从S的头部删除一个字符,加到T的尾部
    •从S的尾部删除一个字符,加到T的尾部
     
    典型的贪心:设立两个指针i、j,分别从字符串S的开头和末尾往中间扫描,比较i、j两处字符的字符,取较小者加入到T中;如果i、j两处字符相同的话则把i、j往中间移动并比较,把较小者的那一端的字符取出加入到T中。输出的时候注意每80个字符占据一行。
     
     
     
    #include <cstdio>
    
    int n;
    char s[2005];
    
    int main()
    {
    
        scanf("%d", &n);
        for(int i = 0; i < n; ++i){
            getchar();
            scanf("%c", &s[i]);
        }
        int cnt = 0;
        for(int i = 0, j = n-1, k = 0; i <= j; ){
            while(s[i+k] == s[j-k] && (i+k) <= (j-k))   //相同时往中间扫
                ++k;
            if(s[i+k]<s[j-k])
                putchar(s[i++]);
            else
                putchar(s[j--]);
            ++cnt;
            if(cnt%80 == 0) //每80个字符占据一行
                putchar('
    ');
            k = 0;
        }
        putchar('
    ');
        return 0;
    }
    
  • 相关阅读:
    .net验证是否合法邮箱和ip地址的方式
    .net通用类型转换方法
    asp.net中的<%%>的使用
    autofac初识
    .net面试题
    asp.net使用一般处理程序实现文件下载
    asp.net 一般处理程序接收上传文件的问题
    Python学习日记(十八) 序列化模块
    Python学习日记(十七) os模块和sys模块
    Python学习日记(十六) time模块和random模块
  • 原文地址:https://www.cnblogs.com/inmoonlight/p/5705198.html
Copyright © 2020-2023  润新知