算法题目-配置文件恢复

58 阅读3分钟

www.nowcoder.com/practice/ca…

描述

有6条配置命令,它们执行的结果分别是:

命   令执   行
resetreset what
reset boardboard fault
board addwhere to add
board deleteno board at all
reboot backplaneimpossible
backplane abortinstall first
he heunknown command

注意:he he不是命令。

为了简化输入,方便用户,以“最短唯一匹配原则”匹配(注:需从首字母开始进行匹配):

1、若只输入一字串,则只匹配一个关键字的命令行。例如输入:r,根据该规则,匹配命令reset,执行结果为:reset what;输入:res,根据该规则,匹配命令reset,执行结果为:reset what;
2、若只输入一字串,但匹配命令有两个关键字,则匹配失败。例如输入:reb,可以找到命令reboot backpalne,但是该命令有两个关键词,所有匹配失败,执行结果为:unknown command

3、若输入两字串,则先匹配第一关键字,如果有匹配,继续匹配第二关键字,如果仍不唯一,匹配失败。

例如输入:r b,找到匹配命令reset board 和 reboot backplane,执行结果为:unknown command。

例如输入:b a,无法确定是命令board add还是backplane abort,匹配失败。

4、若输入两字串,则先匹配第一关键字,如果有匹配,继续匹配第二关键字,如果唯一,匹配成功。例如输入:bo a,确定是命令board add,匹配成功。
5、若输入两字串,第一关键字匹配成功,则匹配第二关键字,若无匹配,失败。例如输入:b addr,无法匹配到相应的命令,所以执行结果为:unknow command。
6、若匹配失败,打印“unknown command”

注意:有多组输入。

数据范围:数据组数:1≤t≤800 1≤t≤800 ,字符串长度1≤s≤20 1≤s≤20 

进阶:时间复杂度:O(n) O(n) ,空间复杂度:O(n) O(n) 

输入描述:

多行字符串,每行字符串一条命令

输出描述:

执行结果,每条命令输出一行

示例1

输入:

reset
reset board
board add
board delet
reboot backplane
backplane abort

复制

输出:

reset what
board fault
where to add
no board at all
impossible
install first

实现

import java.util.*;
import java.util.stream.Collectors;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static Map<String,String> configMap = new HashMap<>();
    public static Set<String> configSetOne = new HashSet<>();
    public static Set<String> configSetTwo = new HashSet<>();
    static {
/**
 *  * reset	reset what
 *  * reset board	board fault
 *  * board add	where to add
 *  * board delete	no board at all
 *  * reboot backplane	impossible
 *  * backplane abort	install first
 *  * he he	unknown command
 */
    configMap.put("reset","reset what");
    configMap.put("reset board","board fault");
    configMap.put("board add","where to add");
    configMap.put("board delete","no board at all");
    configMap.put("reboot backplane","impossible");
    configMap.put("backplane abort","install first");
    configSetOne = configMap.keySet().stream().filter(str->str.split(" ").length==1).collect(Collectors.toSet());
    configSetTwo = configMap.keySet().stream().filter(str->str.split(" ").length==2).collect(Collectors.toSet());
    }
    public static final String UNKNOWN_COMMAND = "unknown command";
    public static void main(String[] args) {
        run();
    }

    private static void run() {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String str = in.nextLine();
            String result = configFileRecovery(str);
            System.out.println(result);
        }
    }

    private static String configFileRecovery(String str) {
        String[] split = str.split(" ");
        int configCount=0;
        if (split.length==1) {
            String temp = "";
            for (String configStr : configSetOne) {
                if(configStr.startsWith(str)){
                    configCount++;
                    temp = configMap.get(configStr);
                }
            }
            if(configCount==1){
                return temp;
            }
        }else if(split.length==2){
            String temp = "";
            for (String configTwo : configSetTwo) {
                String[] configTwoArr = configTwo.split(" ");
                if(configTwoArr[0].startsWith(split[0])){
                    if(configTwoArr[1].startsWith(split[1])){
                        temp = configMap.get(configTwo);
                        configCount++;
                    }
                }
            }
            if(configCount==1){
                return temp;
            }
        }
        return UNKNOWN_COMMAND;
    }
}