牛客[编程题] HJ66 配置文件恢复

发布时间 2023-11-11 23:11:16作者: 张德长

HJ66 配置文件恢复

 
中等  通过率:36.66%  时间限制:1秒  空间限制:32M
 
 

描述

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

 

    using System;
    using System.Collections.Generic;
    public class Program
    {
        public static void Main()
        {
            var cmds = GetCommands();
            var hehe = GetHehe();
            string line;List<string> lines = new List<string>();
            while ((line = System.Console.ReadLine()) != null)
            { // 注意 while 处理多个 case
               var match= GetMatch(line,cmds,hehe);
                Console.WriteLine(match.output);
            }
        }
        static List<Command> GetCommands()
        {
            List<Command> commands = new List<Command>();
            commands.Add(new Command("reset", "reset what") );
            commands.Add(new Command("reset board", "board fault") );
            commands.Add(new Command("board add", "where to add") );
            commands.Add(new Command("board delete", "no board at all") );
            commands.Add(new Command("reboot backplane", "impossible") );
            commands.Add(new Command("backplane abort", "install first") );
            return commands;
        }
        static Command GetHehe()
        {
            return new Command("he he", "unknown command");
        }
        static Command GetMatch(string input, List<Command> cmds, Command hehe)
        {
            List<Command> matches = new List<Command>();
            var arr = input.Split(' ');
            foreach (var c in cmds)
            {
                bool match = true;
                if (c.cmd.Count != arr.Length)
                {
                    continue;
                }
                for (int i = 0; i < arr.Length; i++)
                {
                    for (int j = 0; j < arr[i].Length; j++)
                    {
                        if (arr[i][j]!=c.cmd[i][j])
                        {
                            match = false;
                            break;
                        }
                    }
                    if (!match)
                    {
                        break;
                    }

                }
                if (match)
                {
                    matches.Add(c);
                }
                
            }
            if (matches.Count==1)
            {
                return matches[0];
            }
            return hehe;
        }
    }
//    命 令   执 行
//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
    public class Command
    {
      public  List<string> cmd ;
      public  string output;
        public Command(string cmd,string output)
        {
            this.cmd = new List<string>();
            foreach (var item in cmd.Split(' '))
            {
                this.cmd.Add(item);
            }
            this.output = output;
        }
       
    }