java题目 HJ66 配置文件恢复

描述

有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\le t\le 800\1t800 ,字符串长度1\le s\le 20\1s20 
进阶:时间复杂度: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

 

 1 import java.io.*;
 2 import java.util.*;
 3 
 4 public class Main {
 5     public static void main(String[] args) throws IOException {
 6         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 7         String s;
 8         while((s = br.readLine()) != null) {
 9             recover(s);
10         }
11     }
12     
13     public static void recover(String s) {
14         String[] origin = {"reset","reset board","board add","board delete","reboot backplane","backplane abort","he he"};
15         Map<String, String> map = new HashMap<>();
16         map.put("reset", "reset what");
17         map.put("reset board", "board fault");
18         map.put("board add", "where to add");
19         map.put("board delete", "no board at all");
20         map.put("reboot backplane", "impossible");
21         map.put("backplane abort", "install first");
22         map.put("he he", "unknown command");
23         
24         String[] input = s.split(" ");  //输入的字符串拆分
25             int count = 0; // 匹配的次数
26             String key = ""; // 匹配成功的key
27             //输入长度为1
28             if(input.length == 1){
29                 String cmd = origin[0].substring(0, input[0].length());   //从reset中截取输入字符串的长度,与输入的字符串比较
30                 if(cmd.equals(input[0])){
31                     key = origin[0];
32                     count++;
33                 } else {
34                     key = origin[6];
35                 }
36             } else {  //输入长度不为1
37                 //长度大于2时,不用匹配
38                 if(input.length > 2) {
39                     key = origin[6];
40                 } else { //对两个输入的字符串进行匹配
41                     String input1 = input[0], input2 = input[1];
42                     //与origin中的每个字符串匹配比较
43                     for(int i =1; i<origin.length; i++) {
44                         //将origin中的两个词拆分
45                         String[] two = origin[i].split(" ");
46                         //如果输入的比标准库中的还长不用匹配
47                         if(input1.length() > two[0].length() || input2.length() > two[1].length()) {
48                             continue;
49                         }
50                         String cmd1 = two[0].substring(0, input1.length());  //从two的第一个字符串中截取出 input1的长度
51                         String cmd2 = two[1].substring(0, input2.length());  //从two的第二个字符串中截取出 input1的长度
52                         if(cmd1.equals(input1) && cmd2.equals(input2)) {
53                             count++;
54                             key = origin[i];
55                         }else {
56                             continue;
57                         }
58                     }
59                 }
60             }
61             
62             //遍历了origin中的每一个key,判断匹配成功的次数
63             if(count == 1) {
64                 System.out.println(map.get(key));
65             } else {
66                 System.out.println(map.get("he he"));
67             }
68     }
69 }

 

posted @ 2022-03-13 00:25  海漠  阅读(220)  评论(0编辑  收藏  举报