题解 | #配置文件恢复#

配置文件恢复

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");
    }
}

全部评论

相关推荐

我的人生算是废了,23届裸辞空档一年,存款只能坚持几个月了,找不到像样的工作了,人生何去何从。
梦想是成为七海千秋:这大环境下为什么要裸辞呀,风险真的挺大的,而且社招的话23届没有太多的竞争力,不过既然已经裸辞了就不要焦虑慢慢找。
点赞 评论 收藏
分享
有担当的灰太狼又在摸鱼:零帧起手查看图片
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-19 14:46
和女友两个人马上毕业,现在我在鹅实习995,周六日偶尔也去北京;她在北京金融007,经常忙到后半夜,周末也没啥休息机会两个人现在都不咋聊天了,一句话隔半小时甚至半天才回。&nbsp;她是个很优秀的妹子,工作也很努力,是值得学习一辈子的人。我在努力工作求转正,即便不行至少赚到了一段不错的实习经历。已经异地了半年,接下来可能还会持续是这个状态。我们都算是对方重要的人,只是感觉看上去不是很有未来的样子。希望牛友们给点的鼓励
梦旅奇缘:很难。异地首先就已经很难了,加上妹子是金融行业,忙碌高压,对情感需求很高,而且见惯纸醉金迷,你的很多优势在她那里可能就不算什么了。这种情况下,在她们那里遇到一个能及时照顾她的人,即使那人可能很多条件不如你,你也有可能被分手。 说白了,两个卷王就不太适合在一起。因为卷王最大的优势,在另一个卷王那里就不算优势了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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