Best Cow Line-贪心策略

Posted nuist__NJUPT

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Best Cow Line-贪心策略相关的知识,希望对你有一定的参考价值。

Best Cow Line

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

import java.util.Scanner;

public class MinString {
    public static void f(String s){
        int N = s.length() ;
        int count = 0 ;
        String s1 = new StringBuilder(s).reverse().toString() ;
        StringBuilder result = new StringBuilder() ;
        while(result.length() < N){
            if(s.compareTo(s1) > 0){
                result.append(s1.charAt(0)) ;
                s1 = s1.substring(1) ;
            }else{
                result.append(s.charAt(0)) ;
                s = s.substring(1) ;
            }
            if(result.length() % 80 == 0){
                System.out.println(result.substring(count*80, (count+1)*80)) ;
                count ++ ;
            }
        }
            if(result.length() > count * 80){
                System.out.println(result.substring(80*count)) ;
            }
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in) ;
        int n = input.nextInt() ;
        char [] a = new char [n] ;
        for(int i=0; i<n; i++){
            a[i] = input.next().charAt(0) ;
        }
        f(new String(a)) ;
    }
}

以上是关于Best Cow Line-贪心策略的主要内容,如果未能解决你的问题,请参考以下文章

Best Cow Line---POJ 3617(贪心)

POJ3617:Best Cow Line (贪心&&后缀数组)

poj3617 Best Cow Line(贪心,字典序问题)

POJ 3617 Best Cow Line (贪心)

poj 3623 Best Cow Line, Gold 后缀数组 + 贪心

1640: [Usaco2007 Nov]Best Cow Line 队列变换|后缀数组|贪心