首页 > 试题广场 >

配置文件恢复

[编程题]配置文件恢复
  • 热度指数:113636 时间限制: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.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        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");
        map.put("he he", "unknown command");
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            if (map.containsKey(str)) {
                System.out.println(map.get(str));
            } else {
                System.out.println(fixStr(str, map));
            }
        }
    }
    public static String fixStr(String str, Map<String, String> map) {
        String[] arr = str.split(" ");
        if (arr.length == 1 && "reset".indexOf(arr[0]) == 0) {
            return "reset what";
        } else if(arr.length == 2){
            int legalcount = 0;
            String res = "";
            for (Map.Entry<String, String> entry : map.entrySet()) {
                String[] keyArr = entry.getKey().split(" ");
                if(keyArr.length != 2)
                    continue;
                if(keyArr[0].indexOf(arr[0]) == 0 && keyArr[1].indexOf(arr[1]) == 0){
                    legalcount++;
                    res = entry.getValue();
                }
            }
            return legalcount == 1 ? res : "unknown command";
        }
        return "unknown command";
    }
}
发表于 2025-08-31 15:38:43 回复(0)
先用map<Key, Value>存储完整命令→输出的关系,首先判断输入的字符串序列的字符串个数,①若为1,看“reset”是否该改字符串开头,是则输出map.get("reset"),不是则返回unkonw
②若为2,遍历每一个key,看输入key的第1~2个字符串是否分别以第1~2个输入的字符串开头,是则记录找到结果+1,遍历结束的结果为1则输出值,否则unkonw,
③若为3,没有符合条件的,不需要遍历key了,直接输出unkonw即可。
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");
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String[] strs = in.nextLine().split(" ");
            if (strs.length == 1){
                if ("reset".startsWith(strs[0])){
                    System.out.println("reset what");
                }else System.out.println("unknown command");
            }else if (strs.length == 2){
                int count = 0;
                String res = " ";
                for (String key : map.keySet()){
                    String[] keys = key.split(" ");
                    if (keys.length == 1){
                        continue;
                    }else{
                        if (keys[0].startsWith(strs[0]) && keys[1].startsWith(strs[1])){
                            count++;
                            res = map.get(key);
                        }
                    }
                }
                if (count != 1){
                    System.out.println("unknown command");
                }else{
                    System.out.println(res);
                }
            }else System.out.println("unknown command");
        }
    }

发表于 2025-07-25 11:25:48 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static String RESET = "reset";
    public static String UNKNOWN_COMMAND = "unknown command";
    public static Map 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");
        }
    };
    public static List<String> list = new
    ArrayList<String>() {//命令列表,存储除了reset的其他两个字符串的命令
        {
            add("reset board");
            add("board add");
            add("board delete");
            add("reboot backplane");
            add("backplane abort");
        }
    };
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            String[] arr = a.split(" ");
            if (arr.length > 2 ||
                    arr.length <= 0) {//长度不正确,不是正确的指令
                System.out.println(UNKNOWN_COMMAND);
                continue;
            } else if (arr.length == 1) {//长度为1时,与rest去匹配
                if (check(arr[0], RESET)) {
                    System.out.println(map.get(RESET));
                } else {
                    System.out.println(UNKNOWN_COMMAND);
                }
                continue;
            } else {//其他命令按照两个的匹配
                boolean flag = false;
                String command = null;
                for (int i = 0; i < list.size(); i++) {//按照命令列表去匹配
                    String[] commands = list.get(i).split(" ");
                    if (check(arr[0], commands[0]) &&
                            check(arr[1], commands[1])) {//检查两个命令都匹配
                        if (flag) { //如果之前已经有匹配的命令,且再次匹配了一次,说明重复匹配,命令有问题
                            flag = false;
                            break;
                        }
                        flag = true; //如果两个字符串的命令都匹配成功,设置flag,让后续可以以此来做判断
                        command = list.get(i); //记录此时的命令
                    }
                }
                if (flag) { //如果只有一个命令匹配,则输出这个命令
                    System.out.println(map.get(command));
                } else {//如果没有命令匹配或者匹配了多个命令,则输出未知命令
                    System.out.println(UNKNOWN_COMMAND);
                }
            }
        }
    }
    //检查命令是否符合要求
    public static boolean check(String input, String command) {
        if (input.length() >
                command.length()) {//如果是输入的命令长度超过配置的命令,则返回false
            return false;
        } else if (input.length() == command.length()) {//命令长度相同的情况
            if (input.equals(
                        command)) {//命令完全一致,则命令为正确的,否则为错误的
                return true;
            } else {
                return false;
            }
        }
        //一个个字符进行对比,按照输入的命令的长度进行比较,如果比较出不一样的则返回错误,如果比较完都是一致的,说明这个输入的命令符合要求,返回true
        for (int i = 0 ; i < input.length(); i++) {
            if (input.charAt(i) != command.charAt(i)) {
                return false;
            }
        }
        return true;
    }
}

发表于 2025-07-09 17:52:30 回复(0)
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)