首页 > 试题广场 >

字符串的处理

[编程题]字符串的处理
  • 热度指数:34 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

定义字符串的以下几种操作:

  • reverse(A)获得A字符串的逆序字符串,例如reverse("abc") = "cba"
  • shuffle(A)获得A随机重排的字符串,例如shuffle("dog") ∈ {"dog", "dgo", "odg", "ogd", "gdo", "god"}
  • merge(A1, A2),合并A1和A2两个字符串,合并过程中只保证A1和A2本身字母的顺序,例如merge("qwe", "asd")的可能结果有很多, “qweasd”和“qwased”都是可能的取值。现在给定一个字符串S,S ∈merge(reverse(A), shuffle(A))。求以字母表顺序排序的A的最小值。

输入描述:
输入一个字符串S。


输出描述:
输出一个字符串,为A的最小取值。
示例1

输入

abcdefgabcdefg

输出

agfedcb

说明

提示:reverse("agfedcb") = "bcdefga"shuffle("agfedcb") = "abcdefg"merge("bcdefga", "abcdefg") = "abcdefgabcdefg"
case通过率为80.00%
用例:
qwfkljcqwfkjcj
对应输出应该为:
cfjkwqz
你的输出为:
cjfkwq
测试用例有问题!!!
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();

        Map<Character, Integer> countMap = new HashMap<Character, Integer>();
        for(int i = 0;i<str.length();i++) {
            Integer count = countMap.get(str.charAt(i));
            countMap.put(str.charAt(i), count == null ? 1 : (count + 1));
        }
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        Map<Character, Integer> resMap = new HashMap<Character, Integer>();
        StringBuilder sb = new StringBuilder();

        int i=str.length() - 1;
        int startIndex = i;
        for(;i>=0;i--) {
            Integer count = map.get(str.charAt(i));
            if(count == null
               || count < countMap.get(str.charAt(i)) / 2
               || resMap.get(str.charAt(i)) != null
                   && resMap.get(str.charAt(i)) >= countMap.get(str.charAt(i)) / 2) {
                   map.put(str.charAt(i), count == null ? 1 : (count + 1));
            } else {
                char min = str.charAt(i);
                int minId = startIndex;
                int j;
                startIndex = i - 1;
                do {
                    min = str.charAt(i);
                    j = minId;
                    for(;j>i;j--) {
                        if(str.charAt(j) < min
                           && (map.get(str.charAt(j)) == null
                               || resMap.get(str.charAt(j)) == null
                               || resMap.get(str.charAt(j)) < countMap.get(str.charAt(j)) / 2)
                          ) {
                            min = str.charAt(j);
                            minId = j - 1;
                        }
                    }
                    if(min < str.charAt(i)) {
                        sb.append(min);
                        resMap.put(min, resMap.get(min) == null ? 1 : (resMap.get(min) + 1));
                    }
                } while(min < str.charAt(j));
                sb.append(str.charAt(i));
                resMap.put(str.charAt(i), resMap.get(str.charAt(i)) == null ?
                           1 : (resMap.get(str.charAt(i)) + 1));
                map.put(str.charAt(i), count + 1);
                if(map.size() >= str.length() / 2) {
                    i--;
                    break;
                }
            }
        }
        for(;i>=0;i--) {
            if(resMap.get(str.charAt(i)) == null
               || resMap.get(str.charAt(i)) < countMap.get(str.charAt(i)) / 2){
                sb.append(str.charAt(i));
            }
        }
        System.out.println(sb.toString());
    }
}


发表于 2020-03-08 12:23:10 回复(0)