题解 | #配置文件恢复#

配置文件恢复

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


import java.util.*;

public class Main {
    public static void main(String[] args) {
        // 配置文件恢复
        Scanner sc = new Scanner(System.in);
        Map<String,String> command = new HashMap<>();
        // 将命令行和对应的作用写入map集合
        command.put("reset","reset what");
        command.put("reset board","board fault");
        command.put("board add","where to add");
        command.put("reboot backplane","impossible");
        command.put("backplane abort","install first");
        command.put("board delete","no board at all");
        // 将key写入Set<String[]>
        Set<String[]> set = new HashSet<>();
        for(String s: command.keySet()){
           set.add(s.split(" "));
        }
//        Iterator<String[]> iterator = set.iterator();
//        int index = 0;
//        while(iterator.hasNext()){
//            if(index == 0) {iterator.next();index++;}
//            else {
//                System.out.println(iterator.next()[1]);
//                index++;
//            }
//        }
        while(sc.hasNext()){
            // 处理输入
            String[] res  = sc.nextLine().split(" ");// 空格分隔,并且返回字符串数组
            String comands = null;
            // 匹配处理
            for(String[] strArray : set){
                // 如果输入的串的长度是1
                if(res.length == 1){
                    // 只匹配长度为1的
                    if(strArray.length == 1){
                        if(strArray[0].startsWith(res[0])){
                            comands = strArray[0];// 将comands赋值
                            break;
                        }
                    }
                }else if(res.length == 2){

                    // 长度为2.优先匹配第一个关键字
                    if(strArray.length == 2){
                        if(strArray[0].startsWith(res[0])){
                            if(strArray[1].startsWith(res[1]))
                            {
  
                                comands = strArray[0] + " "+strArray[1];
                                break;
                            }
                        }
                    }
                }
            }
            if(comands != null){
                System.out.println(command.get(comands));
            }else System.out.println("unknown command");
        }
        //
    }
}

全部评论

相关推荐

兄弟们,实习都是在接各种api,该怎么包装简历
仁者伍敌:感觉我自己做小项目也是各种api啊,我要怎么包装简历
点赞 评论 收藏
分享
湫湫湫不会java:先投着吧,大概率找不到实习,没实习的时候再加个项目,然后把个人评价和荣誉奖项删了,赶紧成为八股战神吧,没实习没学历,秋招机会估计不多,把握机会。或者说秋招时间去冲实习,春招冲offer,但是压力会比较大
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-03 18:22
投了几百份简历,专业和方向完全对口,都已读不回。尝试改了一下学校,果然有奇效。
steelhead:这不是很正常嘛,BOSS好的是即便是你学院本可能都会和聊几句,牛客上学院本机会很少了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务