首页 > 试题广场 >

配置文件恢复

[编程题]配置文件恢复
  • 热度指数:111124 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

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

命   令 执   行
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

注意: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

输入

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.ArrayList;
import java.util.List;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        List<String> onecommand = new ArrayList<>();
        List<String> oneresult = new ArrayList<>();
        onecommand.add("reset");
        oneresult.add("reset what");

        List<String> twocommand = new ArrayList<>();
        List<String> twotwo = new ArrayList<>();
        List<String> tworesult = new ArrayList<>();
        twocommand.add("reset");
        twocommand.add("board");
        twocommand.add("board");
        twocommand.add("reboot");
        twocommand.add("backplane");

        twotwo.add("board");
        twotwo.add("add");
        twotwo.add("delete");
        twotwo.add("backplane");
        twotwo.add("abort");

        tworesult.add("board fault");
        tworesult.add("where to add");
        tworesult.add("no board at all");
        tworesult.add("impossible");
        tworesult.add("install first");

        while (in.hasNextLine()) {
            int c = 0;
            int count = 0;
            boolean wrong = false;
            String b = in.nextLine().trim();
            if (b.isEmpty()) {
                continue;
            }
            String[] a = b.split("\\s+");
            if (a.length == 1) {
                if (onecommand.get(0).startsWith(a[0])) {
                    System.out.println(oneresult.get(0));
                    wrong = true;
                }
                if (!wrong) {
                System.out.println("unknown command");
            }
            } else {
                for (int i = 0; i < twocommand.size(); i++) {
                    if (twocommand.get(i).startsWith(a[0]) && twotwo.get(i).startsWith(a[1])) {
                        count++;
                        c = i;
                        wrong = true;
                    }
                }
                if (count == 1) {
                    System.out.println(tworesult.get(c));
                } else {
                    System.out.println("unknown command");
                }
            }
           
        }
    }
}
发表于 2025-03-22 01:26:38 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<String,String> map = new HashMap<String,String>(){{
            put("reset","reset what");
            put("reset board","board fault");
            put("board add","where to add");
            put("board delete","no board at all");
            put("reboot backplane","impossible");
            put("backplane abort","install first");
        }};

        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            String[] strs = str.split(" ");
            Set<String> set = map.keySet();
            int count = 0;
            String command = "unknown command";
            for(String s:set){
                boolean flag = true;
                String[] ss = s.split(" ");
                if(ss.length != strs.length){
                    continue;
                }
                for(int j=0;j<strs.length;j++){
                    if(!ss[j].startsWith(strs[j])){
                        flag = false;
                        break;
                    }
                }
                if(flag){
                    count++;
                    command = s;
                }
            }
            if(count != 1){
                System.out.println("unknown command");
            }else{
                System.out.println(map.get(command));
            }
        }
    }
}
发表于 2024-12-28 19:37:50 回复(0)
//解题思路:
//1.只接用字符串的startsWith()方法就行了
import java.util.*;
public class Main{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine())
        {
            String cmd = sc.nextLine();
            int match_count = 0;
            String operate_str = "unknown command";
            //匹配每一个配置命令
            String[] cmds_array = cmd.split(" ");
            if(cmds_array.length==1)
            {
                if("reset".startsWith(cmds_array[0])==true)
                {
                    match_count++;
                    operate_str = "reset what";
                }
            }
            if(cmds_array.length==2)
            {
                if("reset".startsWith(cmds_array[0])&&"board".startsWith(cmds_array[1]))
                {
                    match_count++;
                    operate_str = "board fault";
                }
                if("board".startsWith(cmds_array[0])&&"add".startsWith(cmds_array[1]))
                {
                    match_count++;
                    operate_str = "where to add";
                }
                if("board".startsWith(cmds_array[0])&&"delete".startsWith(cmds_array[1]))
                {
                    match_count++;
                    operate_str = "no board at all";
                }
                if("reboot".startsWith(cmds_array[0])&&"backplane".startsWith(cmds_array[1]))
                {
                    match_count++;
                    operate_str = "impossible";
                }
                if("backplane".startsWith(cmds_array[0])&&"abort".startsWith(cmds_array[1]))
                {
                    match_count++;
                    operate_str = "install first";
                }
            }
            //输出结果
            if(match_count>1)
            {
                operate_str = "unknown command";
            }
            System.out.println(operate_str);
        }
    }
}

发表于 2024-11-28 14:37:29 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String cmd = in.nextLine();
            String[] outputs = {"reset what", "board fault", "where to add",
                                "no board at all", "impossible", "install first"};
            int ret = match(cmd);
            if (ret == -1) {
                System.out.println("unknown command");
            } else {
                System.out.println(outputs[ret]);
            }
        }
    }

    public static int match(String cmd) {
        String[] correct = {"reset", "reset board", "board add", "board delete",
                            "reboot backplane", "backplane abort"};
        String[] mat = cmd.split(" ");
        int count = 0, index = -1;  // 存储匹配到的数量和索引
        for (int i = 0; i < correct.length; i++) {
            String[] reg = correct[i].split(" ");
            boolean flag = true;  // 初始化
            if (reg.length != mat.length) {
                flag = false;
                continue;
            }
            for (int j = 0; j < reg.length; j++) {
                if (mat[j].length() > reg[j].length()) {
                    flag = false;
                    break;
                }
                if (!reg[j].substring(0, mat[j].length()).equals(mat[j])) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                count++;
                if (count == 1) {
                    index = i;
                } else {
                    index = -1;
                }
            }
        }
        return index;
    }
}
发表于 2024-10-01 21:46:37 回复(0)
没有技巧,就是一顿干码代码
  public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        HashMap<String, String> commands = new HashMap<>();
        commands.put("reset", "reset what");
        commands.put("reset board", "board fault");
        commands.put("board add", "where to add");
        commands.put("board delete", "no board at all");
        commands.put("reboot backplane", "impossible");
        commands.put("backplane abort", "install first");

        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String line = in.nextLine();
            if (line.isEmpty()) {
                continue;
            }
            String[] split = line.split(" ");
            Set<String> curSet = commands.keySet();
            for (int i = 0; i < split.length; i++) {
                String command = split[i];
                int finalI = i;
                curSet = curSet.stream().filter(x -> {
                    String[] s = x.split(" ");
                    if ((s.length - 1 >= finalI) && (split.length == s.length) && s[finalI].startsWith(command)) {
                        return true;
                    } else {
                        return false;
                    }
                }).collect(Collectors.toSet());

            }

            if (curSet.size() == 1) {
                System.out.println(commands.get(curSet.stream().findFirst().get()));
            } else {
                System.out.println("unknown command");
            }

        }
    }


发表于 2024-09-25 17:46:13 回复(0)
//暴力判断法,理条件加敲代码搞半个钟头
import java.util.HashMap;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        HashMap<String, String> hashMap = new HashMap<>();//构建指令表
        hashMap.put("reset", "reset what");
        hashMap.put("reset board", "board fault");
        hashMap.put("board add", "where to add");
        hashMap.put("board delete", "no board at all");
        hashMap.put("reboot backplane", "impossible");
        hashMap.put("backplane abort", "install first");
        hashMap.put("he he", "unknown command");
        while (sc.hasNext()) {
            String instruct = sc.nextLine();//输入的指令
            if (!instruct.contains(" ")) { //只有一个单词的指令情况
                if ("reset".contains(instruct)) {
                    System.out.println(hashMap.get("reset"));
                } else {
                    System.out.println(hashMap.get("he he"));
                }
            } else { //两个指令的情况
                String[] split = instruct.split(" ");
                String instruct1 = split[0];
                String instruct2 = split[1];
                //分割指令
                //先处理冲突的情况,例如:r b;b a;re b;这三种冲突
                //只有一个没有冲突情况,即b d情况输出正常,其余情况全部输出呵呵
                if (instruct1.length() == 1 && instruct2.length() == 1) {
                    if (instruct1.equals("b") && instruct2.equals("d")) {
                        System.out.println(hashMap.get("board delete"));
                    } else {
                        System.out.println(hashMap.get("he he"));
                    }
                } else {
                    if (instruct1.equals("re") && instruct2.equals("b")) {
                        System.out.println(hashMap.get("he he"));
                    } else {
                        if ("reset".contains(instruct1) && "board".contains(instruct2)) {
                            System.out.println(hashMap.get("reset board"));
                            continue;
                        }
                        if ("board".contains(instruct1) && "add".contains(instruct2)) {
                            System.out.println(hashMap.get("board add"));
                            continue;
                        }
                        if ("board".contains(instruct1) && "delete".contains(instruct2)) {
                            System.out.println(hashMap.get("board delete"));
                            continue;
                        }
                        if ("reboot".contains(instruct1) && "backplane".contains(instruct2)) {
                            System.out.println(hashMap.get("reboot backplane"));
                            continue;
                        }
                        if ("backplane".contains(instruct1) && "abort".contains(instruct2)) {
                            System.out.println(hashMap.get("backplane abort"));
                        } else {
                            System.out.println(hashMap.get("he he"));
                        }
                    }
                }
            }
        }
    }
}
发表于 2024-09-21 22:34:51 回复(0)
//贴个自己写的,应该算是比较好理解的了


import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] cmds1 = new String[]{"reset","reset","board","board","reboot","backplane"};
        String[] cmds2 = new String[]{" ","board","add","delete","backplane","abort"};
        String[] operations = new String[]{"reset what","board fault",
        "where to add","no board at all","impossible","install first"};
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String[] str = in.nextLine().split(" ");
            
            String cmd1 = str[0],cmd2 = str.length==1?" ":str[1];
            
            int count = 0,res_idx = -1;
            for(int idx = 0;idx<6;idx++){
                if(cmds1[idx].startsWith(cmd1)&&cmds2[idx].startsWith(cmd2)){
                    count++;
                    if(count>1){
                        System.out.println("unknown command");
                        break;
                    }
                    res_idx = idx;
                }
            }
            if(count==0)  System.out.println("unknown command");
            if(count==1)  System.out.println(operations[res_idx]);
        }
    }
}


编辑于 2024-03-09 19:42:57 回复(1)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<String, String> map = new HashMap();
        map.put("reset", "reset what");
        map.put("reset board", "board fault");
        map.put("board add", "where to add");
        map.put("board delete", "no board at all");
        map.put("reboot backplane", "impossible");
        map.put("backplane abort", "install first");
        String err = "unknown command";
        while(in.hasNextLine()){
            String [] cmd = in.nextLine().split(" ");
            String result = "";
            for(Map.Entry<String, String> entry : map.entrySet()){
                String [] m = entry.getKey().split(" ");
                boolean flag = false;
                if(m.length == cmd.length){
                   if(cmd.length == 1){
                        flag = m[0].startsWith(cmd[0]);
                   }else if(cmd.length == 2){
                        flag = m[0].startsWith(cmd[0]) && m[1].startsWith(cmd[1]);
                   }
                }
                //匹配多个
                if(flag && result != ""){
                    result = err;
                    break;
                }else if(flag){
                    result = entry.getValue();
                }
            }
            System.out.println(result == "" ? err : result);
        }
    }
}

发表于 2023-11-26 15:46:13 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[][] ins = new String[][] {{"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"}};
        while(in.hasNextLine()){
            handleInput(in.nextLine(),ins);
        }
    }

    private static void handleInput(String input,String[][] ins){
        String[] info=input.split(" ");
        if(info.length==1){
            if(matched(info[0],ins[0][0])){
                System.out.println(ins[0][2]);
                return;
            }
        }else if(info.length==2){
            int index=0;
            for(int i=1;i<ins.length;i++){
                if(matched(info[0],ins[i][0])){
                    if(matched(info[1],ins[i][1])){
                        if(index==0){
                            index=i;
                        }else{
                            index=0;
                            break;
                        }
                    }
                }
            }
            if(index>0){
                System.out.println(ins[index][2]);
                return;
            }
            
        }
        System.out.println("unknown command");
    }

    private static boolean matched(String input,String target){
        if(input.length()<=target.length()){
            if(target.substring(0,input.length()).equals(input)){
                return true;
            }
        }
        return false;
    }
}

发表于 2023-09-08 16:41:16 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Map<String,String> map=new HashMap<>();
        map.put("reset","reset what");map.put("reset board","board fault");
        map.put("board add","where to add");map.put("board delete","no board at all");
        map.put("reboot backplane","impossible");map.put("backplane abort","install first");
        Set<String[]> set=new HashSet<>();
        for(String s:map.keySet()){
            set.add(s.split(" "));
        }
        while(sc.hasNextLine()){
            String[] strs=sc.nextLine().split(" ");
            String ss=null;
            int time=0;
            for(String[] s:set){
                if(strs.length==1){
                    if(s.length!=1) continue;
                    else{
                        if(s[0].startsWith(strs[0])){
                            ss=s[0];
                            time++; 
                        }
                        continue;
                    }
                }
                if(strs.length==2){
                    if(s.length!=2) continue;
                    else{
                        if(s[0].startsWith(strs[0])&&s[1].startsWith(strs[1])){
                            ss=s[0]+" "+s[1];
                            time++;
                        }
                        continue;
                    }
                }
            }
            System.out.println(time!=1?"unknown command":map.get(ss));
        }
    }
}

发表于 2023-07-17 09:38:23 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        Map<String,String> map = new HashMap<>();
        map.put("reset","reset what");
        map.put("reset board","board fault");
        map.put("board add","where to add");
        map.put("board delete","no board at all");
        map.put("reboot backplane","impossible");
        map.put("backplane abort","install first");
        Set<String> set = map.keySet();

        while (in.hasNextLine()) {
            String[] input = in.nextLine().split(" ");
            String command = "";

            //遍历命令
            for(String s : set){
                String[] com = s.split(" ");

                if(com.length == 1){
                    if(input.length==1&& com[0].startsWith(input[0])){
                        command =com[0];
                        break;
                    }
                }else{
                    if(input.length==2 && com[0].startsWith(input[0])&& com[1].startsWith(input[1])){
                        //使用+=避免多次匹配
                        command += s;
                    }
                }
            }

            String result = map.get(command);
            if(result==null){
                System.out.println("unknown command");
            }else{
                System.out.println(result);
            }
        }
    }
}

发表于 2023-05-10 21:29:44 回复(0)
import java.util.Scanner;
import java.util.ArrayList;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {

    private static final String[] comondArray1 = {"reset", "board", "board", "reboot", "backplane"};
    private static final String[] comondArray2 = {"board", "add", "delete", "backplane", "abort"};
    private static final String[] excuteArray = {"board fault", "where to add", "no board at all",
    "impossible", "install first"};
   
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<String> commonds = new ArrayList<>();
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            commonds.add(in.nextLine());
        }
        ArrayList<String> executeResults = getExecuteResult(commonds);
        for (String result : executeResults) {
            System.out.println(result);
        }
    }

    private static ArrayList<String> getExecuteResult(ArrayList<String> commonds) {
        ArrayList<String> executeResults = new ArrayList<>();
        for (String commond : commonds) {
            String[] commondArr = commond.split(" ");
            if (commondArr.length == 1) {
                if ("reset".startsWith(commondArr[0])) {
                    executeResults.add("reset what");
                } else {
                    executeResults.add("unknown command");
                }
            } else if (commondArr.length == 2) {
                int count = 0;
                String temp = "";
                for (int i = 0; i < comondArray1.length; i++) {
                    if (comondArray1[i].startsWith(commondArr[0]) && comondArray2[i].startsWith(commondArr[1])) {
                        temp = excuteArray[i];
                        count++;
                    }
                }
                if (count==1) {
                     executeResults.add(temp);
                } else {
                    executeResults.add("unknown command");
                }
            } else {
                executeResults.add("unknown command");
            }
        }
        return executeResults;
    }

}
发表于 2023-04-25 17:22:22 回复(0)
Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            Map<String,String> map = new HashMap<String,String>();
            map.put("reset","reset what");
            map.put("reset board","board fault");
            map.put("board add","where to add");
            map.put("board delete","no board at all");
            map.put("reboot backplane","impossible");
            map.put("backplane abort","install first");
            map.put("he he","unknown command");

            List<String> list = new ArrayList<String>();
            list.add("reset board");
            list.add("board add");
            list.add("board delete");
            list.add("reboot backplane");
            list.add("backplane abort");

            String[] strs = str.split(" ");
            if(strs.length==1){
                if("reset".indexOf(strs[0])!=0){
                    System.out.println(map.get("he he"));
                }else{
                    System.out.println(map.get("reset"));
                }
            }else{
                String resultCmd = "";
                int flag = 0;
                for(int i=0;i<list.size();i++){                  
                     String cmd = list.get(i);
                     String[] cmdStrs = cmd.split(" ");
                    if(cmdStrs[0].indexOf(strs[0])==0 && cmdStrs[1].indexOf(strs[1])==0){
                        resultCmd = cmd;
                        flag++;
                    }  
                }
                if(flag==1){
                     System.out.println(map.get(resultCmd));
                }else{
                    System.out.println(map.get("he he"));
                }
            }
        }
发表于 2023-04-16 20:31:17 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String key[] = new String[] {"reset", "reset board", "board add",
                                     "board delete", "reboot backplane", "backplane abort"
                                    };
        String value[] = new String[] {"reset what", "board fault", "where to add",
                                       "no board at all", "impossible", "install first"
                                      };
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String s = in.nextLine();
            String command[] = s.split(" ");
            int len = command.length;
            if (len ==
                    1) { //若只输入一字串,则只匹配一个关键字的命令行
                if ("reset".startsWith(command[0])) {
                    System.out.println("reset what");
                } else {
                    System.out.println("unknown command");
                }
            } else {
                int matchNum = 0;
                String matchValue = "";
                for (int i = 0; i < 6; i++) {
                    if (key[i].startsWith(command[0])) {
                        if (key[i].contains(" ")) {
                            String keys[] = key[i].split(" ");
                            if (keys[1].startsWith(command[1])) {
                                matchNum++;
                                matchValue = value[i];
                            }
                        }
                    }
                }
                if (matchNum == 1) {
                    System.out.println(matchValue);
                } else {
                    System.out.println("unknown command");
                }

            }
        }
    }
}

发表于 2023-03-13 13:41:00 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("reset", "reset what");
        hashMap.put("reset board", "board fault");
        hashMap.put("board add", "where to add");
        hashMap.put("board delete", "no board at all");
        hashMap.put("reboot backplane", "impossible");
        hashMap.put("backplane abort", "install first");

//        LinkedHashMap<String, String> map = new LinkedHashMap<>();

        while (in.hasNextLine()) {
            String key = in.nextLine(); //AA BB
            //对输入的字串按" "拆分,根据数组长度判断输入的是1字串还是2字串
            String[] array = key.split(" ");
            //若为1字串,则从hashmap中的key第一关键字匹配,只能有1次,否则报错
            Set<String> keySet = hashMap.keySet();
            String searchKey = "";
            int flag = 0;
            for (String hashKey : keySet) {
                //判断输入的是1字串还是2字串
                String[] hashMapKeyArray = hashKey.split(" "); ////backplane abort
                if (array.length == 1  && hashMapKeyArray.length == 1) {
                    //取出hmap中的key并匹配第一关键字
                    if (hashMapKeyArray[0].indexOf(array[0]) == 0) {
                        flag++;
                        searchKey = hashKey;
                    }
                } else if (array.length == 2 &&
                           hashMapKeyArray.length == 2) { //输入的是是2字串
                    if (hashMapKeyArray[0].indexOf(array[0]) == 0 &&
                            hashMapKeyArray[1].indexOf(array[1]) == 0) {
                        searchKey = hashKey;
                        flag++;
                    }
                }
            }
            if (flag == 1) {
                System.out.println(hashMap.get(searchKey));
            } else {
                System.out.println("unknown command");
            }
        }
    }
}
发表于 2023-03-06 03:46:09 回复(0)
import java.util.*;

public class Main {
    static String[] coms = {"reset","reset board","board add","board delete","reboot backplane",
                            "backplane abort"};
    static String[] ress = {"reset what","board fault","where to add","no board at all",
                            "impossible","install first"};
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNextLine()){
            List<String> list = new ArrayList<>();
            //输入的命令
            String[] com = in.nextLine().split(" ");
            
            for(int i = 0;i < coms.length;i++){
                String[] temp = coms[i].split(" ");
                if(com.length == 1){
                    if(temp.length == 2){
                        continue;
                    }
                    if(temp[0].startsWith(com[0])){
                        list.add(ress[i]);
                        break;
                    }
                }
                
                if(com.length == 2){
                    if(temp.length == 1){
                        continue;
                    }else if(temp[0].startsWith(com[0]) && temp[1].startsWith(com[1])){
                        list.add(ress[i]);
                    }
                }
            }
            
            if(list.size() > 1 || list.size() == 0){
                System.out.println("unknown command");
            }else{
                System.out.println(list.get(0));
            }
            
        }
    }
}

发表于 2022-09-13 14:14:39 回复(0)
直接用的正则匹配
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

/**
 * @author YXQ
 * @create 2022/8/11  22:39
 */
public class Main {
    static HashMap<String,String> map=new HashMap();
    public static void main(String[] args) throws IOException {
        map.put("reset","reset what");
        map.put("reset board","board fault");
        map.put("board add","where to add");
        map.put("board delete","no board at all");
        map.put("reboot backplane","impossible");
        map.put("backplane abort","install first");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String str=null;
        while ((str= br.readLine())!=null){
            System.out.println(findMatch(str));
        }
        br.close();
    }
    public static String findMatch(String str){
        String res="unknown command";
        int count=0;
        String[] strs=str.split(" ");
        String regex="";
        for(int i=0;i<strs.length;i++){
            regex=regex+strs[i]+"[a-z]* ";
        }
        regex=regex.substring(0,regex.length()-1);

//        System.out.println(regex);
        for(String key:map.keySet()){
            if(key.matches(regex)){
                if(count==0){
                    res=map.get(key);
                    count++;
                }else{
                    res="unknown command";
                }
            }
        }
        return res;

    }
}


发表于 2022-08-13 11:49:04 回复(0)
/***
乍看复杂,其实将命令分成几类就可以简单做好:
第1类:reset : 一个字符
第2类:reset board 和reboot backplane :两个字符且首字母不能同时满足
第3类:board add 和 backplane abort:两个字符且首字母不能同时满足
第4类:board delete:首字母分别满足即可
第5类:其他:unknown command
运行时间:96ms
占用内存:14048KB
***/import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) { 
            String str = sc.nextLine();
            System.out.println(transfer(str));
        }
    }
    
    public static String transfer(String str){
        //先通过是否有空格来来判断是一个还是两个字符
        if(!str.contains(" ")){        //无空格:一个字符,匹配reset即可
            if("reset".startsWith(str)){
                return "reset what";
            }else{
                return "unknown command";
            }
        }else{                         //有空格:两个及以上字符
            String[] string = str.split(" ");
            if(string.length >2){         //大于两个字符,无匹配
                return "unknown command";
            }else{                        //两个字符,继续判断
                String s1 = string[0];
                String s2 = string[1];
                //除reset的其他4类情况一一比较
                if("reset".startsWith(s1) && "board".startsWith(s2) && !"backplane".startsWith(s2)){
                    return "board fault";
                }else if("reset".startsWith(s1) && "board".startsWith(s2) && "reboot".startsWith(s1) && "backplane".startsWith(s2)){
                    return "unknown command";  
                }else if(!"board".startsWith(s2) && "reboot".startsWith(s1) && "backplane".startsWith(s2)){
                    return "impossible";
                }else if("board".startsWith(s1) && "add".startsWith(s2) && "backplane".startsWith(s1) && "abort".startsWith(s2)){
                    return "unknown command";                    
                }else if("board".startsWith(s1) && "add".startsWith(s2) && (!"backplane".startsWith(s1) || !"abort".startsWith(s2))){
                    return "where to add";
                }else if(!"add".startsWith(s2) && "backplane".startsWith(s1) && "abort".startsWith(s2)){
                    return "install first";
                }else if("board".startsWith(s1) && "delete".startsWith(s2)){
                    return "no board at all";
                }else{
                    return "unknown command";
                }      
            }
        }
    }
}

发表于 2022-05-04 22:04:08 回复(0)
 public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] part1 = {"reset","board","board","reboot","backplane"};
        String[] part2 = {"board","add","delete","backplane","abort"};
        String[] result = {"board fault","where to add","no board at all","impossible","install first"};
        while(scanner.hasNext()) {
            String[] input = scanner.nextLine().split(" ");
            if(input.length==1){
                if(input[0].matches("re?s?e?t?")){
                    System.out.println("reset what");
                    continue;
                }
                System.out.println("unknown command");
            }else{
                //匹配第一部分命令
                int[] part1Index = new int[5];
                int part1num=0;
               for(int i=0;i<5;i++){
                   if(stringMatch(input[0],part1[i])){
                       part1Index[i]++;
                       part1num++;
                   }
               }
               if(part1num==0){
                   System.out.println("unknown command");
                   continue;
               }
               //匹配第二次命令
               int index=0;
               int num=0;
               for(int i=0;i<5;i++){
                   if(part1Index[i]==1){
                       if(stringMatch(input[1],part2[i])){
                           index=i;
                           num++;
                       }
                   }
               }
               if(num==1) System.out.println(result[index]);
               else System.out.println("unknown command");
            }
        }
    }
    public  static boolean stringMatch(String input,String target){
        for(int i=0;i<input.length();i++){
            if(input.charAt(i)!=target.charAt(i)) return false;
        }
        return true;
    }

发表于 2022-04-17 11:45:46 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        List<String> cmds = new ArrayList<>(8);
        while (in.hasNextLine()) {
            cmds.add(in.nextLine());
        }
        List<String> results = execute(cmds);
        for (String s : results) {
            System.out.println(s);
        }
    }
    
    private static String UNKNOWN = "unknown command";
    private static final List<Cmd> list;
    static {
        list = new ArrayList<>();
        list.add(new Cmd("reset", "reset what"));
        list.add(new Cmd("reset board", "board fault"));
        list.add(new Cmd("board add", "where to add"));
        list.add(new Cmd("board delete", "no board at all"));
        list.add(new Cmd("reboot backplane", "impossible"));
        list.add(new Cmd("backplane abort", "install first"));
    }
    
    private static class Cmd {
        String key1;
        String key2;
        int len;
        String src;
        String dest;
        Cmd(String src, String dest) {
            String[] splits = src.split(" ");
            key1 = splits[0];
            len = splits.length;
            if (len > 1) {
                key2 = splits[1];
            }
            this.src = src;
            this.dest = dest;
        }
        
        public boolean match(String src) {
            String[] ss = src.split(" ");
            if (ss.length > 1) {
                return mt(ss[0],ss[1]);
            } else {
                return mt(ss[0]);
            }
        }
         
        private boolean mt(String k) {
            return key2 == null && key1.startsWith(k);
        }
        
        private boolean mt(String k, String k2) {
            return key2 != null && key1.startsWith(k) && key2.startsWith(k2);
        }        
    }
    
    private static List<String> execute(List<String> srcs) {
        List<String> result = new ArrayList<>();
        for (String src : srcs) {
            String dest = null;
            for (Cmd cmd : list) {
                if (c***tch(src)) {
                    if (dest == null) {
                        dest = cmd.dest;
                    } else {
                        dest = UNKNOWN;
                        break;
                    }
                }
            }
            if (dest == null) {
                dest = UNKNOWN;
            }
            result.add(dest);
        }
        return result;
    }
                 
}

发表于 2022-03-21 23:21:37 回复(0)