HUAWEI OD computer test questions -- message reordering

/**
* Retransmitting and reordering messages Q is a commonly used reliability mechanism. There are a certain number of sub-messages in the retransmission buffer area, and the order of each sub-message in the original message is known. Now The original message needs to be recovered.
*
* Input description
* The first line of input is N, indicating the number of sub-messages.
*
* Enter the second line of N sub-messages, separated by spaces, and the format of the sub-messages is:
*
* Character content of the message + suffix sequence index
*
* The content of the string message consists of [az,AZ], and the suffix is ​​an integer Type value, indicating the order. The sequence value is unique and does not repeat.
*
*Output description
* Output the restored original message, and restore the original message in ascending order according to the order of each sub-message, and the sequence suffix needs to be deleted from the recovered message* Input* *
4
*
rolling3
stone4 like1 a2
* output
*
* 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));
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_42450130/article/details/131488635