题解 | #配置文件恢复#
配置文件恢复
http://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
import java.util.*;
public class Main{
private static ArrayList<String> list = new ArrayList<>();
private static HashMap<String,String> map = new HashMap<>();
private static void init(){
list.add("reset");
list.add("reset board");
list.add("board add");
list.add("board delete");
list.add("reboot backplane");
list.add("backplane abort");
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("he he","unknown command");
}
public static String getCommand(String str){
String retStr = map.get("he he");
String[] strs = str.split(" ");
if(strs.length==1){
String reset = list.get(0);
String cmd = strs[0];
if(reset.indexOf(cmd)==0){
retStr = map.get(reset);
}
}else if(strs.length==2){
String first = strs[0];
String sec = strs[1];
int count = 0;
for(int i=1;i<list.size();i++){
String s = list.get(i);
String[] ss = s.split(" ");
if(ss[0].indexOf(first)==0 && ss[1].indexOf(sec)==0){
count++;
retStr = map.get(s);
}
}
if(count!=1){
retStr = map.get("he he");
}
}
return retStr;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
init();
while(sc.hasNextLine()){
String str = sc.nextLine();
System.out.println(getCommand(str));
}
}
}
public class Main{
private static ArrayList<String> list = new ArrayList<>();
private static HashMap<String,String> map = new HashMap<>();
private static void init(){
list.add("reset");
list.add("reset board");
list.add("board add");
list.add("board delete");
list.add("reboot backplane");
list.add("backplane abort");
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("he he","unknown command");
}
public static String getCommand(String str){
String retStr = map.get("he he");
String[] strs = str.split(" ");
if(strs.length==1){
String reset = list.get(0);
String cmd = strs[0];
if(reset.indexOf(cmd)==0){
retStr = map.get(reset);
}
}else if(strs.length==2){
String first = strs[0];
String sec = strs[1];
int count = 0;
for(int i=1;i<list.size();i++){
String s = list.get(i);
String[] ss = s.split(" ");
if(ss[0].indexOf(first)==0 && ss[1].indexOf(sec)==0){
count++;
retStr = map.get(s);
}
}
if(count!=1){
retStr = map.get("he he");
}
}
return retStr;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
init();
while(sc.hasNextLine()){
String str = sc.nextLine();
System.out.println(getCommand(str));
}
}
}