题解 | #配置文件恢复#
配置文件恢复
http://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
import java.util.*;
public class Main{
static Map<String, String> map = new HashMap<String, String>(){
{
put("reset", "reset what");
put("reset board", "board fault");
put("board add", "where to add");
put("board delete", "no board at all");
put("reboot backplane", "impossible");
put("backplane abort", "install first");
}
};
public static void main(String []args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String cmd = sc.nextLine();
calculate(cmd);
}
}
public static void calculate(String cmd) {
String[] cmdArr = cmd.split(" ");
if (cmdArr.length == 1) {
if ("reset".contains(cmd)) {
System.out.println(map.get("reset"));
} else {
System.out.println("unknown command");
}
} else {
String result = "unknown command";
int count = 0;
for (String key : map.keySet()) {
if (key.equals(cmd)) {
result = map.get(key);
break;
} else {
String[] keyArr = key.split(" ");
if (keyArr.length > 1 && keyArr[0].indexOf(cmdArr[0]) == 0 && keyArr[1].indexOf(cmdArr[1]) == 0) {
result = map.get(key);
count++;
}
}
}
System.out.println(count > 1 ? "unknown command" : result);
}
}
}