题解 | #配置文件恢复#
配置文件恢复
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();
}}
