题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
// mark 一下 // 按照题目意思 每种情况都进行处理即可 import java.util.*; public class Main { private static final String UNKNOW_COMMAND = "unknown command"; public static void main(String[] args) { Scanner in = new Scanner(System.in); Map<String, String> map = new HashMap<>(); 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"); while (in.hasNextLine()) { String s = in.nextLine(); if (s.contains(" ")) { String[] strings = s.split(" "); if (strings.length == 2) { String first = strings[0]; String second = strings[1]; int matchNum = 0; String matchKey = ""; for (String eachKey : map.keySet()) { String[] keys = eachKey.split(" "); if (keys[0].startsWith(first) && keys[1].startsWith(second)) { matchNum++; matchKey = eachKey; } } if (matchNum == 1) { System.out.println(map.get(matchKey)); } else { System.out.println(UNKNOW_COMMAND); } } else { System.out.println(UNKNOW_COMMAND); } } else { if ("reset".startsWith(s)) { System.out.println("reset what"); } else { System.out.println(UNKNOW_COMMAND); } } } } }