题解 | #配置文件恢复#

配置文件恢复

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

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Scanner;
import java.util.Set;

public class Main {

    public static final Map<Command, String> ONE_KEYWORD_COMMAND_TO_RESULT = new
    LinkedHashMap<>();
    public static final Map<Command, String> TWO_KEYWORD_COMMAND_TO_RESULT = new
    LinkedHashMap<>();

    public static final String UNKNOWN_COMMAND = "unknown command";

    static {
        ONE_KEYWORD_COMMAND_TO_RESULT.put(new Command(1, "reset"), "reset what");
        TWO_KEYWORD_COMMAND_TO_RESULT.put(new Command(2, "reset", "board"),
                                          "board fault");
        TWO_KEYWORD_COMMAND_TO_RESULT.put(new Command(2, "board", "add"),
                                          "where to add");
        TWO_KEYWORD_COMMAND_TO_RESULT.put(new Command(2, "board", "delete"),
                                          "no board at all");
        TWO_KEYWORD_COMMAND_TO_RESULT.put(new Command(2, "reboot", "backplane"),
                                          "impossible");
        TWO_KEYWORD_COMMAND_TO_RESULT.put(new Command(2, "backplane", "abort"),
                                          "install first");
    }

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

        while (in.hasNextLine()) {

            String commandString = in.nextLine();

            String result = null;

            Command command = getCommand(commandString);

            String value1 = ONE_KEYWORD_COMMAND_TO_RESULT.get(command);
            String value2 = TWO_KEYWORD_COMMAND_TO_RESULT.get(command);

            if (value1 != null) {
                result = value1;
            } else if (value2 != null) {
                result = value2;
            } else {
                int keywordCount = command.keywordCount;
                if (keywordCount == 1) {
                    Set<Command> commands = ONE_KEYWORD_COMMAND_TO_RESULT.keySet();
                    for (Command c : commands) {
                        if (c.getValue1().contains(command.getValue1())) {
                            result = ONE_KEYWORD_COMMAND_TO_RESULT.get(c);
                        }
                    }
                    if (result == null) {
                        result = UNKNOWN_COMMAND;
                    }
                }
                if (keywordCount == 2) {
                    Set<Command> commands = TWO_KEYWORD_COMMAND_TO_RESULT.keySet();
                    Set<Command> matchedCommands = new LinkedHashSet<>();
                    for (Command c : commands) {
                        if (c.getValue1().indexOf(command.getValue1()) == 0) {
                            matchedCommands.add(c);
                        }
                    }
                    int count = 0;
                    String firstKeyword = null;
                    String secondKeyword = null;
                    if (!matchedCommands.isEmpty()) {
                        for (Command c : matchedCommands) {
                            if (c.getValue2().indexOf(command.getValue2()) == 0) {
                                count++;
                                firstKeyword = c.value1;
                                secondKeyword = c.value2;
                            }
                        }
                        if (count != 1) {
                            result = UNKNOWN_COMMAND;
                        } else {
                            Command c = new Command(2, firstKeyword, secondKeyword);
                            result = TWO_KEYWORD_COMMAND_TO_RESULT.get(c);
                        }

                    } else {
                        result = UNKNOWN_COMMAND;
                    }

                }
            }

            System.out.println(result);

        }

        in.close();
    }

    public static Command getCommand(String command) {
        String[] strings = command.split(" ");
        int length = strings.length;
        if (length == 2) {
            return new Command(length, strings[0], strings[1]);
        } else {
            return new Command(length, strings[0]);
        }
    }

    public static class Command {
        private int keywordCount;
        private String value1;
        private String value2;

        public Command(int keywordCount, String value1, String value2) {
            this.keywordCount = keywordCount;
            this.value1 = value1;
            this.value2 = value2;
        }

        public Command(int keywordCount, String value1) {
            this.keywordCount = keywordCount;
            this.value1 = value1;
        }

        public int getKeywordCount() {
            return keywordCount;
        }

        public void setKeywordCount(int keywordCount) {
            this.keywordCount = keywordCount;
        }

        public String getValue1() {
            return value1;
        }

        public void setValue1(String value1) {
            this.value1 = value1;
        }

        public String getValue2() {
            return value2;
        }

        public void setValue2(String value2) {
            this.value2 = value2;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Command command = (Command) o;
            return Objects.equals(value1, command.value1)
                   && Objects.equals(value2, command.value2);
        }

        @Override
        public int hashCode() {
            return Objects.hash(value1, value2);
        }
    }

}

全部评论

相关推荐

11-13 20:16
已编辑
厦门理工学院 软件测试
专业嗎喽:硕佬,把学校背景放后面几段,学校背景双非还学院,让人看了就不想往下看。 把实习经历和个人奖项放前面,用数字化简述自己实习的成果和掌握的技能,比如负责项目一次通过率90%,曾4次发现项目潜在问题风险为公司减少损失等等
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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