题解 | #配置文件恢复#

配置文件恢复

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

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.script.ScriptException;

public class Main {
    private static HashMap<String, String> map = new HashMap<>();
    static {
        map.put("reset", "reset what");
        map.put("reset board", "board fault");
        map.put("board add", "where to add");
        map.put("board delete", "no board at all");
        map.put("reboot backplane", "impossible");
        map.put("backplane abort", "install first");
    }
    private static int[] length=new int[]{1,2,2,2,2,2};
    public static void main(String[] args) throws ScriptException {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String s = in.nextLine();
            String[] strs = s.split(" ");
            String p;
            if (strs.length == 1) {
                p = "^" + strs[0] + "[^ ]*$";
            } else if (strs.length == 2) {
                p = "^" + strs[0] + ".* " + strs[1] +".*"+ "$";
            } else {
                System.out.println("unknown command");
                return;
            }
            Pattern pattern = Pattern.compile(p);
            int count = 0;
            String res = "";
            for (Map.Entry<String, String> entry : map.entrySet()) {
                String value = entry.getKey();
                Matcher matcher = pattern.matcher(value);
                if (matcher.matches()) {
                    count++;
                    res = entry.getValue();
                }
                if(count==1&&strs.length==1)
                    break;
            }
            if (count == 1) {
                System.out.println(res);
            } else {
                System.out.println("unknown command");
            }
        }
        in.close();
    }
}
全部评论

相关推荐

02-01 19:48
门头沟学院 Java
神哥了不得:(非引流)直接暑期吧,没时间日常了,老鱼简历把水印去了,或者换个模板,简历字体大小都不太行,建议换2个高质量的项目,面试应该还会再多一些
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务