题解 | #配置文件恢复#

配置文件恢复

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

参考别人的
import java.util.*;

public class Main {

public static void main(String[] args) {
    Map<String, String> map = new HashMap<String, String>() {{
        put("reset", "reset what");
        put("reset board", "board fault");
        put("board add", "where to add");
        put("reboot backplane", "impossible");
        put("backplane abort", "install first");
        put("board delete", "no board at all");
        put("noMatch", "unknown command");
    }};
    Set<String[]> set = new HashSet<String[]>();
    for (String s : map.keySet()) {
        set.add(s.split(" "));
    }

    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
        String ans = "noMatch";
        int count = 0;
        String[] ss = sc.nextLine().split(" ");
        for (String[] keys : set) {
            if (ss.length == 1) {
                if (keys.length == 1 && keys[0].startsWith(ss[0])) {
                    ans = keys[0];
                    break;
                }
            } else if (ss.length == 2) {
                if (keys.length == 2 && keys[0].startsWith(ss[0]) && keys[1].startsWith(ss[1])) {
                    ans = keys[0] + " " + keys[1];
                    count++;
                }
            } else { //其他长度的都不对
                break;
            }
        }
        System.out.println(count > 1 ? map.get("noMatch") : map.get(ans));
    }
    sc.close();
}

}

全部评论

相关推荐

03-29 18:59
运城学院 Java
程序员小白条:咱们要对自己的简历和学历有清晰的认知,不要动不动就大厂了....都26届了,没实习还想着大厂,唉
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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