题解 | #配置文件恢复#

配置文件恢复

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

import java.util.*;
import java.util.regex.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        // while (in.hasNextInt()) { // 注意 while 处理多个 case
        //     int a = in.nextInt();
        //     int b = in.nextInt();
        //     System.out.println(a + b);
        // }
        String[] commands = {"reset", "reset board", "board add", "board delete", "reboot backplane", "backplane abort"};
        String[] results = {"reset what", "board fault", "where to add", "no board at all", "impossible", "install first", "unknown command"};

        while (in.hasNextLine()) {
            String command = in.nextLine();
            String[] subs = command.split(" ");
            int sublen = subs.length;
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < sublen; i++) {
                if (i == 0) {
                    builder.append("^" + subs[i] + "[a-z]" + "*");
                } else {
                    builder.append(" +" + subs[i] + "[a-z]" + "*");
                }
            }
            command = builder.toString();
            Pattern pattern = Pattern.compile(command);
            int count = 0;
            int index = -1;
            for (int i = 0; i < commands.length; i++) {
                String s = commands[i];
                Matcher matcher = pattern.matcher(s);
                if (matcher.find()) {
                    if (sublen == 1 && !s.contains(" ")) {
                        count++;
                        index = i;
                        break;
                    } else if (sublen > 1) {
                        count++;
                        index = i;
                    }
                }
            }
            if (count == 1 && index != -1) {
                System.out.println(results[index]);
            } else {
                System.out.println(results[6]);
            }
        }
    }
}

全部评论

相关推荐

每天投完简历等邮箱,邮箱一亮我满心欢喜打开。。。
wish233:还有每次有电话,都会想是那家公司约面了,接起来:家长您好
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 评论 收藏
分享
我只是一个小白菜:我还用不惯m4,也是山猪吃不了细糠了
投递字节跳动等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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