POJ 3617 Best Cow Line (贪心)

Best Cow Line

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

USACO 2007 November Silver

题意:给出一个长为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;
}
时间: 2024-07-28 21:56:24

POJ 3617 Best Cow Line (贪心)的相关文章

POJ 3617 Best Cow Line (贪心)

题意: 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初T是一个空串,随后反复进行下列任意操作 1.从字符串S头部删除一个字符,加到T的尾部 2.从字符串S尾部删除一个字符,加到T的尾部 目的是,构造字典序尽可能小的字符串T 思路:简单贪心,比较当前字符串S首尾字符的大小,选取小的加入T,若两者相同,则比较下一个字符. #include<stdio.h> #include<queue> #include<iostream> #include<algori

poj 3617 Best Cow Line (字符串反转贪心算法)

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

poj 3617 Best Cow Line(贪心)

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

poj 3617 Best Cow Line

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

贪心/POJ 3617 Best Cow Line

1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int main() 5 { 6 int n; 7 char s1[2020],s2[2020]; 8 scanf("%d",&n); 9 for (int i=0;i<n;i++) 10 { 11 char ch; 12 scanf(" %c",&ch); 13 s1[i]=ch; 14 }

POJ 3617 - Best Cow Line(字典序最小) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=3617 洛谷题目链接:https://www.luogu.org/problem/show?pid=2870 题目大意: 给定一个长度为N(1<=N<=30000)的字母序列,每次可以从序列头部或尾部取出一个字母加入一个队列的尾部,求此队列字典序最小的排列. 每输出80个字母,换行一次. 分析: 每次比较序列首部和尾部,取出较小的一个,如果相等就继续往下比较,直

POJ3617 Best Cow Line 贪心

这题虽然简单但是挺不错的,因为过程很好,比较开发思维 和鼓励人,不像有些贪心太偏不好推反而让人厌烦 给出长度为N的字符串S,然后还有一个空串STR,每次有两个选择 1:删除S的头元素假加入STR中      2:删除S的尾元素加入STR中 是的STR字典序最小 并输出 开始可能没有什么顾虑的去想 每次比较S的头和尾元素 取小的那个删除并假如STR中,但是若S的头和尾元素一样的话这个方法就不行了,因为先取头或者尾还得看他们之间的元素,这时候是倒着来还是顺着好呢?那就直接拿顺的跟倒的进行字典序的大小

POJ 3623 Best Cow Line, Gold(模拟)

题意  给你一个字符序列   你每次可以从它的头部或尾部拿出一个字符组成一个新的字符序列   输出这样做能达到的最小的字符序列   每行最多输出80个字符(开始被这个坑了好久) 直接模拟就行  哪边小就选哪边  相等就往内看 #include<cstdio> #include<iostream> #include<string> using namespace std; const int N = 30010; int main() { char s[N][2]; in

POJ 3623 Best Cow Line, Gold(字符串处理)

题意:给你一个字符串,让你重新排列,只能从头或者尾部取出一个放到新字符串队列的最后.按照字典序. 解决方法:比较前后两个的大小,谁小输出谁,相等,就往当中比来确定当前应该拿最前面的还是最后面的,如果再相等就继续.... 所以比较这个动作的单一功能,可以写成一个check函数,方便操作也方便递归. #include<iostream> #include<cstring> using namespace std; #define MAX 30005 char str[MAX]; int