题解 | #配置文件恢复#

配置文件恢复

http://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5

import java.io.*;
import java.util.*;
public class Main{
    public static void main(String[] args)throws Exception{
        BufferedReader br = new  BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        HashMap<String,String> hmap=new HashMap<>();
        hmap.put("reset","reset what");
        hmap.put("reset board","board fault");
        hmap.put("board add","where to add");
        hmap.put("board delete","no board at all");
        hmap.put("reboot backplane","impossible");
        hmap.put("backplane abort","install first");
        String[][] strarr= new String[5][2];
        strarr[0][0]="reset";strarr[0][1]="board";
        strarr[1][0]="board";strarr[1][1]="add";
        strarr[2][0]="board";strarr[2][1]="delete";
        strarr[3][0]="reboot";strarr[3][1]="backplane";
        strarr[4][0]="backplane";strarr[4][1]="abort";
        String str="";
        while((str=br.readLine())!=null){
            String[] arr=str.split(" ");
            int len = arr.length;
            if(len == 1 && pipei(arr[0],strarr[0][0])){
                out.println(hmap.get(strarr[0][0]));
                out.flush();
            }
            else if(len==2){
                int count = 0;
                int index=0;
                for(int i=0;i<strarr.length;i++){
                    if(pipei(arr[0],strarr[i][0]) && pipei(arr[1],strarr[i][1])) {count++;index=i;}
                }
                if(count==1) {
                    String ans=strarr[index][0]+" "+strarr[index][1];
                    out.println(hmap.get(ans));out.flush();
                }
                else{
                    out.println("unknown command");out.flush();
                }
            }
            else{
                out.println("unknown command");out.flush();
            }
        }
    }
    static boolean pipei(String s1,String s2){
        for(int i=0;i<s1.length();i++){
            if(s1.charAt(i) != s2.charAt(i)) return false;
        }
        return true;
    }
}
全部评论

相关推荐

秋盈丶:后续:我在宿舍群里和大学同学分享了这事儿,我好兄弟气不过把他挂到某脉上了,10w+阅读量几百条评论,直接干成精品贴子,爽
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务