最左侧匹配原则匹配关键字Java

package com.patience.interview.huawei;


import java.util.*;

/**
 * 恢复配置文件
 * @author Green.Gee
 * @date 2022/11/16 14:58
 * @email [email protected]
 */
public class recoveryConfigFile {


    static Map<String,String> cmd = new HashMap<>();
    static {
        cmd.put("reset","reset what");
        cmd.put("reset board","board fault");
        cmd.put("board add","where to add");
        cmd.put("board delete","no board at all");
        cmd.put("reboot backplane","impossible");
        cmd.put("backplane abort","install first");
    }

    private static final String UNKNOW = "unknown command";

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String word = in.nextLine();
            if(cmd.containsKey(word)){
                System.out.println(cmd.get(word));
            }else{
                if(word.split(" ").length <= 1){
                    boolean flag = true;
                    for(Map.Entry<String,String> entry : cmd.entrySet()) {
                        String k = entry.getKey();
                        String v = entry.getValue();
                        if(leftFit(k,word.length(),word)){
                            if(k.indexOf(" ") > 0){
                                System.out.println(UNKNOW);
                                flag = true;
                                break;
                            }else{
                                System.out.println(v);
                                flag = true;
                                break;
                            }
                        }else{
                            flag = false;
                        }
                    }
                    if(!flag){
                        System.out.println(UNKNOW);
                    }
                }else{
                    String[] words = word.split(" ");
                    if(words.length > 2){// must be two
                        System.out.println(UNKNOW);
                        break;
                    }else{
                        List<String> keysOne = new ArrayList<>();
                        for(Map.Entry<String,String> entry : cmd.entrySet()) {
                            String k = entry.getKey();
                            String[] kS = k.split(" ");
                            if(leftFit(kS[0],words[0].length(),words[0])){
                                if(kS.length == 2){
                                    keysOne.add(k);
                                }
                            }
                        }
                        if(keysOne.size() == 0){
                            System.out.println(UNKNOW);
                        }else{
                            List<String> keysTwo = new ArrayList<>();
                            for(String item : keysOne){
                                String[] kS = item.split(" ");
                                if(leftFit(kS[1],words[1].length(),words[1])){
                                    keysTwo.add(item);
                                }
                            }
                            if(keysTwo.size() == 0) {
                                System.out.println(UNKNOW);
                            }else{
                                if(keysTwo.size() == 1){
                                    System.out.println(cmd.get(keysTwo.get(0)));
                                }else{
                                    System.out.println(UNKNOW);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 最短唯一匹配原则”匹配(注:需从首字母开始进行匹配)
     */
    private static boolean leftFit(String str,int len,String left){
        if (str == null) {
            return false;
        }
        if (len < 0) {
            return false;
        }
        if (str.length() <= len) {
            return str.equals(left);
        }
        return str.substring(0, len).equals(left);
    }
}

你可能感兴趣的:(算法【,Hard,Code】,java,开发语言)