题解 | #配置文件恢复#

配置文件恢复

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;
    }
}
全部评论

相关推荐

程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
10-22 12:03
山东大学 Java
程序员小白条:26届一般都得有实习,项目可以随便写的,如果不是开源社区的项目,随便包装,技术栈也是一样,所以本质应该找学历厂,多投投央国企和银行,技术要求稍微低一点的,或者国企控股那种,纯互联网一般都得要干活
应届生简历当中,HR最关...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务