字典序最小问题

1. 问题描述:

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

这道题目来着北大的POJ上3617的题目

意思大概是:

给一个定长为N的字符串S,构造一个字符串T,长度也为N。
起初,T是一个空串,随后反复进行下列任意操作
1. 从S的头部删除一个字符,加到T的尾部
2. 从S的尾部删除一个字符,加到T的尾部
目标是最后生成的字符串T的字典序尽可能小
1≤N≤2000
字符串S只包含大写英文字母
输入:字符串S
输出:字符串T
样例输入: ACDBCB

2. 题目的要求是要求我们求出字典序最小的字符组成的字符串,从题目中字典序最小这些字眼,所以我们想到尝试使用贪心策略来解决

接下来就是解决贪心策略的问题,对于这道题目来说,策略比较容易看出,因为就是字典序最小的问题,谁字典序较小那么我们就是加上谁,所以我们比较头尾字符哪一个字符的字典序较小那么就加上哪一个字符,假如头,尾两个字符相同那么继续往下看头尾两个字符看谁的字典序最小那么就加上哪边字典序较小的问题,例如字符串"ACDBCB",那么第一次循环肯定是先把A加进来,然后把B加进来,下一次判断的时候发现头尾都是"C"那么继续往下看头尾两个字符发现B的字典序小于D那么肯定是先加上尾部的这个C的字符的,然后把B加进来,把C加进来,把D进来,最后形成了字典序最小的字符串"ABCBCD"

其中怎么样表示字符的移动呢,这里我们定义两个整形变量,类似于指针,分别指在字符串的头尾,然后循环的过程中进行移动

使用两个循环,最外面的循环表示字符的移动,里面的循环用来查找当指针指向的两个字符相同的时候那么判断需要加入的字典序较小的是哪一边,其中使用到了辅助的两个变量用来进行字符的移动,当找到当前指针指向的字符不相同的时候那么结束内层循环,那么结束的时候就知道相同的字符的下面的字符哪一边的字典序最小

3. 其中特别要注意输出的问题,因为题目规定每80个字符输出然后换行,刚开始的时候没有注意这个提交代上去提示:Presentation Error,表示输出的格式是有问题的,这个时候修改一下就可以了,具体的代码如下:

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < N; i++){
            sb.append(sc.next());
        }
        int left = 0;
        int right = N - 1;
        StringBuilder aim = new StringBuilder();
        while(left <= right){
            char c1 = sb.toString().charAt(left);
            char c2 = sb.toString().charAt(right);
            char ch1 = c1;
            char ch2 = c2;
            int leftCopy = left;
            int rightCopy = right;
            //两个字符相同的情况下使用循环来看这两个字符下一个字符字典序的情况
            while(leftCopy <= rightCopy && ch1 == ch2){
                ch1 = sb.toString().charAt(leftCopy);
                ch2 = sb.toString().charAt(rightCopy);                
                leftCopy++;
                rightCopy--;
            }
            if(ch1 > ch2){
                aim.append(c2 + "");
                right--;
            }else{
                aim.append(c1 + "");
                left++;
            }
        }        
                int times = N / 80;
                int yushu = N % 80;
                //每80个字符输出换行
                for(int i = 0; i < times; i++){
                    System.out.println(aim.toString().substring(i * 80, (i + 1) * 80));
                }
                System.out.println(aim.toString().substring(N - yushu, N));
                sc.close();
    }
}

除了上面这种方法,我们还可以使用另外一种更有技巧性的方法来进行解决,那就是将原来控制台输入的字符串进行翻转,然后在循环中进行两个字符串的比较看一下两个字符串哪个字典序更小那么就加入哪个字符串的首个字符,哪一边加入的就把哪一边的原来的字符串的那个字符删掉,这里可以使用substring方法来截取进行重新赋值...当加入的字符的长度等于目标字符的长度那么结束循环就可以了

具体的代码如下:

import java.util.Scanner;
public class Main{
    //采用的是翻转字符串进行比较的方法,所以来说这个方法更加简单地比较字符串
      public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        StringBuilder ss = new StringBuilder();
        for (int i = 0; i < N; i++) {
          ss.append(sc.next());
        }
        // String s = sc.nextLine();
        f(ss.toString());  
      }
      private static void f(String s) {
        String s1 = new StringBuilder(s).reverse().toString();
        int N = s.length();
        StringBuilder rs = new StringBuilder();
        int cnt = 0;
        while (rs.length() < N) {
          if (s.compareTo(s1) <= 0) {
            rs.append(s.charAt(0));
            s = s.substring(1);
          } else {
            rs.append(s1.charAt(0));
            s1 = s1.substring(1);
          }
          //字符满80个就换行
          if (rs.length() % 80 == 0) {
            System.out.println(rs.substring(cnt * 80, (cnt + 1) * 80));
            cnt++;
          }
        }
        //余数部分
        if (rs.length() > cnt * 80) {
          System.out.println(rs.substring(cnt * 80));
        }
      }
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值