码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3617 Best Cow Line (贪心)

时间:2015-01-22 13:24:57      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11230   Accepted: 3329

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的字符串,有两种操作:1.把串首一个元素移到串尾;2.把串尾一个元素移到串首。问通过任意次的这两种操作可以得到的字典序最小的字符串是什么。


解析:贪心。贪心策略很关键。最先想到的可能是直接从两头扫,直接比较,每次都选择小的那个。这样很接近正解了,可惜当我们两者相等的时候,我们是随便取的,这样得到的结果就可能不是字典序最小的。所以当两字符相等时,我们需要看一下下一对的情况,我们优先选择小的那端的字符,重复此过程,直到不相等为止。

PS:这题有坑,输出时候每行字符个数不能超过80,到80要另开一行,就是输出一个‘\n‘!!!




AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif // sxk

    int n;
    char s[2002];
    while(scanf("%d", &n)!=EOF){
        for(int i=0; i<n; i++) cin>>s[i];      //读入也要注意,scanf好像会读错。。。
        int i = 0, j = n-1;
        int cnt = 0;
        while(i <= j){
            bool left = false;
            for(int k=0; i+k<=j; k++){        //一直找到不相等的
                if(s[i+k] < s[j-k]){
                    left = true;
                    break;
                }
                else if(s[i+k] > s[j-k]){
                    left = false;
                    break;
                }
            }
            if(left) putchar(s[i++]);
            else putchar(s[j--]);
            if(++cnt % 80 == 0) puts("");      //80换行
        }
    }
    return 0;
}






POJ 3617 Best Cow Line (贪心)

标签:

原文地址:http://blog.csdn.net/u013446688/article/details/43016679

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!