注意单词别写错了。。。。。

配置文件恢复

https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5

package com.crl.huawei.h66;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main2 {
    static Map<String, String> map;
    final static String error = "unknown command";

    static {
        // 初始化数据
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("reset", "reset what");
        hashMap.put("reset board", "board fault");
        hashMap.put("board add", "where to add");
        hashMap.put("board delete", "no board at all");
        hashMap.put("reboot backplane", "impossible");
        hashMap.put("backplane abort", "install first");
        map = hashMap;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) {
            String line = in.nextLine();
            String[] command = line.split(" ");
            int count = 0;
            ArrayList<String> keyList = new ArrayList<>();
            for (Map.Entry<String, String> entry : map.entrySet()) {
                boolean ok = true;
                String key = entry.getKey();
                String[] split = key.split(" ");
                if (split.length != command.length) {
                    continue;
                }

                for (int i = 0; i < command.length; i++) {
                    // 数组拿出来的元素
                    String regx = command[i];
                    String string = split[i];
                    // 注意:如resetgg boardkk是不合法的
                    if (regx.length() > string.length()){
                        ok = false;
                        break;
                    }

                    int regxIndex = 0;
                    int stringIndex = 0;
                    while (true) {
                        // 循环到有其中之一比较完了,结束循环
                        if (regxIndex == regx.length() || stringIndex == string.length()) {
                            break;
                        }
                        // 从0开始比较两个字符串
                        if (regx.charAt(regxIndex) != string.charAt(stringIndex)) {
                            ok = false;
                            break;
                        }

                        regxIndex++;
                        stringIndex++;
                    }
                }

                if (ok) {
                    count++;
                    keyList.add(key);
                }
            }

            if (count != 1){
                System.out.println(error);
            }else {
                System.out.println(map.get(keyList.get(0)));
            }

        }
    }


}

全部评论

相关推荐

xxxxOxo:该催就催,想要你的不会因为催就挂,催了就挂的是因为本来就要挂你
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务