题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 /** 1. map 然后循环对比 如果只有一个 那么就输出 否则就报错 */ Map<String,String> map=new HashMap<>(); 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"); while (in.hasNext()) { String cmd=in.nextLine(); String res=""; int count=0; for(String str: map.keySet()){ String[] s1=cmd.split(" "); String[] s2=str.split(" "); if(s1.length!=s2.length){ continue; }else{ for(int i=0;i<s1.length;i++){ if(!s2[i].startsWith(s1[i])){ break; } if(i==s1.length-1){ res=map.get(str); count++; } } } } if(count==1){ System.out.println(res); }else{ System.out.println("unknown command"); } } } }