题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> arry = new ArrayList<>();
while (scanner.hasNext()){
String s = scanner.nextLine();
arry.add(s);
}
for (int i = 0; i <arry.size(); i++) {
System.out.println(reback(arry.get(i)));
}
}
public static String reback(String str) {
LinkedHashMap<String, String> hm1 = new LinkedHashMap<>();
hm1.put("reset", "reset what");
hm1.put("reset board", "board fault");
hm1.put("board add", "where to add");
hm1.put("board delete", "no board at all");
hm1.put("reboot backplane", "impossible");
hm1.put("backplane abort", "install first");
hm1.put("he he", "unknown command");
LinkedHashMap<String, Integer> hm2 = new LinkedHashMap<>();
hm2.put("reset", 0);
hm2.put("reset board", 0);
hm2.put("board add", 0);
hm2.put("board delete", 0);
hm2.put("reboot backplane", 0);
hm2.put("backplane abort", 0);
hm2.put("he he", 0);
String[] split = str.split(" ");
if (split.length == 1) {
int length = split[0].length();
String s = "reset";
String split1 = s.substring(0, split[0].length());
if (split[0].equals(split1)) {
return hm1.get("reset");
} else {
return hm1.get("he he");
}
} else if (split.length == 2) {
String sleft = split[0];
String sright = split[1];
int lengthl = sleft.length();
int lengthr = sright.length();
String key = "";
for (String keys : hm1.keySet()) {
String[] splitk = keys.split(" ");
if (splitk.length==1){
continue;
}
if (lengthl>splitk[0].length()||lengthr>splitk[1].length()){
continue;
}
String subleft = splitk[0].substring(0, lengthl);
String subright = splitk[1].substring(0, lengthr);
if (sleft.equals(subleft) && sright.equals(subright)) {
key = keys;
hm2.put(keys, hm2.get(keys) + 1);
}
}
int count = 0;
for (String keyw : hm2.keySet()) {
count += hm2.get(keyw);
}
if (count == 1) {
return hm1.get(key);
} else {
return hm1.get("he he");
}
}
return hm1.get("he he");
}
}
