题解 | #配置文件恢复#
配置文件恢复
http://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.script.ScriptException;
public class Main {
private static HashMap<String, String> map = new HashMap<>();
static {
map.put("reset", "reset what");
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");
}
private static int[] length=new int[]{1,2,2,2,2,2};
public static void main(String[] args) throws ScriptException {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String s = in.nextLine();
String[] strs = s.split(" ");
String p;
if (strs.length == 1) {
p = "^" + strs[0] + "[^ ]*$";
} else if (strs.length == 2) {
p = "^" + strs[0] + ".* " + strs[1] +".*"+ "$";
} else {
System.out.println("unknown command");
return;
}
Pattern pattern = Pattern.compile(p);
int count = 0;
String res = "";
for (Map.Entry<String, String> entry : map.entrySet()) {
String value = entry.getKey();
Matcher matcher = pattern.matcher(value);
if (matcher.matches()) {
count++;
res = entry.getValue();
}
if(count==1&&strs.length==1)
break;
}
if (count == 1) {
System.out.println(res);
} else {
System.out.println("unknown command");
}
}
in.close();
}
}