华为OD机考真题--报文重排序

/**
* 对报文进行重传和重排序Q是常用的可靠性机制,重传缓中区内有一定数量的子报文,每个子报文在原始报文中的顺序已知,现在需要恢复出原始报文。
*
* 输入描述
* 输入第一行为N,表示子报文的个数,。
*
* 输入第二行为N个子报文,以空格分开,子报文格式为:
*
* 字符审报文内容+后缀顺序索引
*
* 字符串报文内容由[a-z,A-Z]组成,后缀为整型值,表示顺序。 顺序值唯一,不重复。
*
*输出描述
* 输出恢复出的原始报文,按照每人子报文的顺序的升序排序恢复出原始报文,顺序后缀需要从恢复出的报文中删除掉
* 输入
*
* 4
* rolling3 stone4 like1 a2
* 输出
*
* like a rolling stone
*/

public class MessageSort {
    public static void main(String[] args) {
         String [] value = "gifts6 and7 Exchanging1 all2 precious5 thins8 kinds3 of4 a9 and10 b11".split(" ");
         //将数字跟字符串保持在Map的k-v中
        getMap(value);
    }

    private static void getMap(String[] value) {
        HashMap<Integer,String> kv = new HashMap<>();
        for (int i = 0; i < value.length; i++){
            String temp = value[i];
            //2个正则表达式分别取出字符串跟整数值
            Pattern pattern1 = Pattern.compile("\\d+");
            Matcher matcher1 = pattern1.matcher(temp);
            Integer num = 0;
            String str = null;
            if (matcher1.find()){
                 num = Integer.valueOf(matcher1.group());
            }
            Pattern pattern2 = Pattern.compile("[a-zA-z]+");
            Matcher matcher2 = pattern2.matcher(temp);
            if (matcher2.find()){
                 str = matcher2.group();
            }
            kv.put(num,str);
        }
        //遍历输出
        for (int j = 1; j <= value.length; j++){
            System.out.println(kv.get(j));
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42450130/article/details/131488635