题解 | #配置文件恢复#抄大佬代码
配置文件恢复
http://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
import java.util.*;
import java.lang.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
Map<String,String> map=new HashMap<String,String>();
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");
map.put("noMatch","unknown command");
Set<String[]> set=new HashSet<>();
for(String s : map.keySet()){
set.add(s.split(" "));
}
while(sc.hasNext()){
String[] arr=sc.nextLine().split(" ");
String res="noMatch";
int count=0;
for(String[] s:set){
//如果输入的字符串为1个
if(arr.length==1){
//指令个数为2个,跳过
if(s.length==2){
continue;
}
else{
if(s[0].startsWith(arr[0])){
res=s[0];
}
}
}
//如果指令个数为2个,并且只有一个可以匹配
if(arr.length==2){
if(s.length==1){
continue;
}
else{
if(s[0].startsWith(arr[0]) && s[1].startsWith(arr[1])){
res=s[0]+" "+s[1];
count++;
}
}
}
}
System.out.println(count>1?"unknown command":map.get(res));
}
}
}