题解 | #配置文件恢复#

配置文件恢复

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        String[] commands = new String[] {"reset", "reset board", "board add", "board delete", "reboot backplane", "backplane abort", "he he"};
        String[] implts = new String[] {"reset what", "board fault", "where to add", "no board at all", "impossible", "install first", "unknown command"};
        int[] indArr = new int[] {0, 6, 6, 6, 7, 10};
        String a, console = "";
        char[] chs;
        char[] comd;
        int i, j, l, cnt, index, lc, match, matchInx;
        boolean invalid;
        try {
            while ((a = r.readLine()) != null && !a.isEmpty()) {
                chs = a.toCharArray();
                l = chs.length;
                i = 0;
                cnt = 1;
                index = 0;
                while (i < l) {
                    if (chs[i] == ' ') {
                        if (i + 1 < l && chs[i + 1] != ' ') index = i + 1;
                        cnt++;
                    }
                    i++;
                }
                if (cnt == 1) {
                    invalid = false;
                    comd = commands[0].toCharArray();
                    if (l > comd.length) {
                        console = console + implts[6] + "\n";
                        continue;
                    }
                    i = 0;
                    while (i < l) {
                        if (chs[i] != comd[i]) {
                            invalid = true;
                            break;
                        }
                        i++;
                    }
                    if (invalid) console = console + implts[6] + "\n";
                    else console = console + implts[0] + "\n";
                    continue;
                }
                if (cnt > 2) {
                    console = console + implts[6] + "\n";
                    continue;
                }
                i = 1;
                match = 0;//匹配的数量
                matchInx = 6;//匹配的索引
                while (i < 6) {
                    invalid = false;//由于遍历时,一个新的命令开始时,判断重新开始,因此invalid初始为false
                    comd = commands[i].toCharArray();
                    lc = comd.length;
                    if (index > indArr[i] || l - index > lc - indArr[i]) {//不匹配
                        i++;
                        continue;
                    }
                    j = 0;
                    while (j < index - 1) {//第一个单词是否匹配
                        if (chs[j] != comd[j]) {
                            invalid = true;
                            break;
                        }
                        j++;
                    }
                    j = 0;
                    while (j < l -
                            index) {//第二个单词是否匹配,以输入的字符串长度为准,因此为l-index
                        if (chs[index + j] != comd[indArr[i] + j]) {
                            invalid = true;
                            break;
                        }
                        j++;
                    }
                    if (!invalid) {
                        matchInx = i;//匹配索引赋值
                        match++;//匹配数量自加
                    }
                    i++;
                }
                if (match != 1) console = console + implts[6] + "\n";
                else console = console + implts[matchInx] + "\n";
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.print(console);
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 16:15
我应届生,去年10月份开始在这家公司实习,到今年10月份正好一年想(实习+试用期),在想要不要提前9月份就离职,这样好找工作些,但又差一个月满一年,又怕10月份国庆回来离职,容易错过了下半年的金九银十,到年底容易gap到年后
小破站_程序员YT:说这家公司不好吧,你干了快一年 说这家公司好吧,你刚毕业就想跑路说你不懂行情吧,你怕错过金九银十说 你懂行情吧,校招阶段在实习,毕业社招想换工作 哥们,我该怎么劝你留下来呢
应届生,你找到工作了吗
点赞 评论 收藏
分享
风中翠竹:真的真的真的没有kpi。。。面试官是没有任何kpi的,捞是真的想试试看这个行不行,碰碰运气,或者是面试官比较闲现在,没事捞个人看看。kpi算HR那边,但是只有你入职了,kpi才作数,面试是没有的。
双非有机会进大厂吗
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-04 18:02
好不容易拿到了字节Offer,鼠鼠做后端的,但家里人觉得可能被裁员不稳定,让鼠鼠去投国企,现在好纠结到底该咋选
文档传偷助手:该投就投吧,不过建议别放弃offer 拿到手里的才是最好的
投递字节跳动等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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