寒光博客

[POJ]3617 Best Cow Line 贪心
https://cn.vjudge.net/problem/POJ-3617 题目大意 输入一些字符 然后你可...
扫描右侧二维码阅读全文
12
2019/08

[POJ]3617 Best Cow Line 贪心

https://cn.vjudge.net/problem/POJ-3617

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


翻译版

FJ即将带着他的N头(1≤N≤2000)奶牛参加一年一度的“年度农民”大赛。在这场比赛中,每个农民都把他的奶牛排成一排,并把它们赶过评委。
比赛组织者今年采用了一种新的登记方案:只要按照每头奶牛的名字的首字母出现的顺序登记(即:每头奶牛的名字和字母的首字母相同)。,如果FJ按这个顺序接受贝西、西尔维娅和朵拉,他只需注册BSD)。登记阶段结束后,根据奶牛姓名首字母串,按词典递增顺序对每一组进行判断。
FJ今年很忙,必须赶回他的农场,所以他想尽早被评判。他决定重新安排他的奶牛,它们已经排好队了,然后再登记。
FJ标志着一个新的竞争奶牛生产线的位置。然后,他通过不断地将原行的第一头或最后一头奶牛发送到新行末尾,将奶牛从旧行引导到新行。当他完成后,FJ把他的牛登记在这个新订单。
给定他的牛的初始顺序,确定他能用这种方法得到的最小的字母串。

题目大意

输入一些字符 然后你可以从最前 或者最后 依次拿出字符 重新组成新的字符串 并保证其字典序最小, 然后输出结果 每80个字符一行

分析

通过贪心 来确定子问题当前最优解法 不同字符 拿小的 相同字符考虑 下一位的字符 情况。
在相同时候 在考虑下一位 乃至下下一位的的情况 我们可以把字符串翻转 比较 并且每次拿出来的 都删去 这样问题就会更加简单

代码

package POJ;

import java.util.Scanner;

public class _3617_BestCowLine {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < n; i++) {
            s.append(cin.next());
        }
        f(s, n);
    }

    private static void f(StringBuilder s, int n) {
        String s1 = s.toString();
        String s2 = new StringBuilder(s).reverse().toString();

        int cnt = 0;
        StringBuilder rec = new StringBuilder("");
        while (rec.length() < n) {
            if (s1.compareTo(s2) <=0) {
                rec.append(s1.charAt(0));
                s1=s1.substring(1);
            } else {
                rec.append(s2.charAt(0));
                s2=s2.substring(1);
            }
            if(rec.length()%80==0){
                System.out.println(rec.substring(cnt*80,++cnt*80));
            }
        }
        if(rec.length()>cnt*80){
            System.out.println(rec.substring(cnt*80));
        }
    }
}
本文作者:Author:     文章标题:[POJ]3617 Best Cow Line 贪心
本文地址:https://dxoca.cn/Algorithm/247.html       百度未收录
版权说明:若无注明,本文皆为“Dxoca's blog (寒光博客)”原创,转载请保留文章出处。
Last modification:August 13th, 2019 at 01:03 am
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment