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

配置文件恢复

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)));
            }

        }
    }


}

全部评论

相关推荐

不愿透露姓名的神秘牛友
04-30 11:43
春招失败、父母离婚,好像我的人生一团糟,一年来压力大到常常崩溃。不知道能跟谁聊,朋友其实对我非常好,但是她无意中表达出来的家庭幸福都会刺痛到我……和ai聊天,我的未来在更高处,不在楼下,忍不住爆哭😭
youngfa:害,妹妹,我是一个研究生(很上进很想找到好工作的那种),但去年因为生病回家休养错过了秋招(当时对我的冲击也是非常大的),这学期返校来了也是把论文盲审交了后才开始找工作,现在也是一个offer没有,但我就没有像你一样把这个阶段性的事情绑定到人生上,人生不仅很长,也很广阔,先停下来,放松一下哦。不要被外部环境灌输的思维操控了,好好爱自己!
点赞 评论 收藏
分享
点赞 评论 收藏
分享
allin秋招的大菠萝很爱交友:后续,已拿offer ~查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务