题解 | #配置文件恢复#

配置文件恢复

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNextLine()) {
            String s = in.nextLine();
            System.out.println(recover(s));
        }
    }

    public static String recover(String command) {
        String pattern = null;
        int cl = command.split(" ").length;
        for (String key : dict.keySet()) {
            if (cl == 1) {
                if (key.split(" ").length > 1) continue;
                int l1 = 0, l2 = 0;
                for (; l1 < key.length() && l2 < command.length(); ) {
                    if (key.charAt(l1) != command.charAt(l2)) {
                        break;
                    } else {
                        l1++;
                        l2++;
                    }
                }
                if (l2 == command.length()) {
                    if (pattern != null) {
                        return UNKNOW;
                    } else {
                        pattern = dict.get(key);
                    }
                }
            } else {
                if (key.split(" ").length == 1) continue;
                String[] keys = key.split(" ");
                String[] commands = command.split(" ");
                int count = 0;
                for (int i = 0; i < 2; i++) {
                    int l1 = 0, l2 = 0;
                    for (; l1 < keys[i].length() && l2 < commands[i].length(); ) {
                        if (keys[i].charAt(l1) !=  commands[i].charAt(l2)) {
                            break;
                        } else {
                            l1++;
                            l2++;
                        }
                    }
                    if (l2 == commands[i].length()) {
                        count++;
                    }
                }
                if (count == 2) {
                    if (pattern != null) {
                        return UNKNOW;
                    } else {
                        pattern = dict.get(key);
                    }
                }
            }
        }
        return pattern == null ? UNKNOW : pattern;
    }

    static Map<String, String> dict = new HashMap<>();
    final static String UNKNOW = "unknown command";

    static {
        dict.put("reset", "reset what");
        dict.put("reset board", "board fault");
        dict.put("board add", "where to add");
        dict.put("board delete", "no board at all");
        dict.put("reboot backplane", "impossible");
        dict.put("backplane abort", "install first");
    }
}

全部评论

相关推荐

03-18 09:45
莆田学院 golang
牛客749342647号:佬,你这个简历模板是哪个,好好看
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务