[编程题]配置文件恢复

Talk is cheap, show me the code.

一、问题描述

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

reset reset what
reset board board fault
board add where to add
board delet no board at all
reboot backplane impossible
backplane abort install first
he he unkown command

注意:he he不是命令。

为了简化输入,方便用户,以“最短唯一匹配原则”匹配:

1、若只输入一字串,则只匹配一个关键字的命令行。例如输入:r,根据该规则,匹配命令reset,执行结果为:reset what;输入:res,根据该规则,匹配命令reset,执行结果为:reset what;

2、若只输入一字串,但本条命令有两个关键字,则匹配失败。例如输入:reb,可以找到命令reboot backpalne,但是该命令有两个关键词,所有匹配失败,执行结果为:unkown command

3、若输入两字串,则先匹配第一关键字,如果有匹配但不唯一,继续匹配第二关键字,如果仍不唯一,匹配失败。例如输入:r b,找到匹配命令reset board,执行结果为:board fault。

4、若输入两字串,则先匹配第一关键字,如果有匹配但不唯一,继续匹配第二关键字,如果唯一,匹配成功。例如输入:b a,无法确定是命令board add还是backplane abort,匹配失败。

5、若输入两字串,第一关键字匹配成功,则匹配第二关键字,若无匹配,失败。例如输入:bo a,确定是命令board add,匹配成功。

6、若匹配失败,打印“unkown command”

输入描述:

多行字符串,每行字符串一条命令

输出描述:

执行结果,每条命令输出一行

输入例子:

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

二、解题思路

纯逻辑题,不过很容易写得很长很繁琐,先给出一个稍长的解法。

代码:

#include <iostream>
#include <string>
using namespace std;

void test1()
{
    string line;
    while(getline(cin, line))
    {
        if(line.find(" ") != string::npos)
        {
            string cmd1 = line.substr(0, line.find_first_of(" "));
            string cmd2 = line.substr(line.find_first_of(" ")+1);
            if(((cmd1 == "r" || cmd1 == "re") && cmd2 == "b") || (cmd1 == "b" && cmd2 == "a"))
            {
                cout << "unkown command" << endl;
                continue;
            }
            bool flag2 = false;
            if(cmd1[0] == 'r')
            {
                flag2 = true;
                string temp = "reset";
                int num = 0;
                if(temp.find(cmd1) != string::npos)
                {
                    temp = "board";
                    if(cmd2[0] == 'b' && temp.find(cmd2) != string::npos)
                    {
                        cout << "board fault" << endl;
                        num++;
                    } else
                        cout << "unknown command" << endl;
                }
                temp = "reboot";
                if(temp.find(cmd1) != string::npos)
                {
                    temp = "backplane";
                    if(cmd2[0] == 'b' && temp.find(cmd2) != string::npos)
                    {
                        cout << "impossible" << endl;
                        num++;
                    } else
                        cout << "unknown command" << endl;
                }
                if(num != 1)
                    cout << "unknown command" << endl;
            }
            if(cmd1[0] == 'b')
            {
                flag2 = true;
                string temp = "board";
                int num = 0;
                if(temp.find(cmd1) != string::npos)
                {
                    bool flag = false;
                    temp = "add";
                    if(cmd2[0] == 'a' && temp.find(cmd2) != string::npos)
                    {
                        num++;
                        cout << "where to add" << endl;
                        flag = true;
                    }
                    temp = "delet";
                    if(cmd2[0] == 'd' && temp.find(cmd2) != string::npos)
                    {
                        num++;
                        cout << "no board at all" << endl;
                        flag = true;
                    }
                    if(!flag)
                        cout << "unkown command" << endl;
                }
                temp = "backplane";
                if(temp.find(cmd1) != string::npos)
                {
                    temp = "abort";
                    if(cmd2[0] == 'a' && temp.find(cmd2) != string::npos)
                    {
                        cout << "install first" << endl;
                        num++;
                    } else
                        cout << "unkown command" << endl;
                }
                if(num != 1)
                    cout << "unkown command" << endl;
            }
            if(!flag2)
                cout << "unkown command" << endl;
        } else {
            string temp = "reset";
            if(line[0] == 'r' && temp.find(line) != string::npos)
                cout << "reset what" << endl;
            else
                cout << "unkown command" << endl;
        }
    }
}

int main()
{
    test1();
    return 0;
}

再给出一种稍微简短一点的解法:

代码:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
vector<pair<pair<string, string>, string>> helper{    
{{"reset",""},"reset what"},{ {"reset","board"},"board fault" },{ {"board","add"},"where to add" },    { {"board","delet"},"no board at all" }, { {"reboot","backplane"},"impossible" },    { {"backplane","abort"},"install first" } };
int main() {    
    string str;    
    while (getline(cin,str)) {        
        int _count = count(str.begin(), str.end(), ' ');        
        if (_count == 0)            
            if (helper[0].first.first.find(str) != string::npos)                
                cout << helper[0].second << endl;            
            else 
                cout << "unkown command" << endl;       
        else if (_count == 1 && str[str.size() - 1] != ' ') {            
            int flag = 0;            
            string tmp;            
            string part1 = str.substr(0, str.find(' '));            
            string part2 = str.substr(str.find(' ') + 1);            
            for (auto p : helper) {                
                if (p.first.first.find(part1) != string::npos && 
                p.first.second.find(part2) != string::npos) {                    
                    ++flag;                    
                    if (flag == 2) break;                    
                        tmp = p.second;                
                }            
            }           
            if (flag == 1) 
                cout << tmp << endl;            
            else 
                cout<< "unkown command" << endl;        
        }        
        else cout << "unkown command" << endl;    
    }
}

补充的测试用例:

输入:

r b
re b
b a

输出:

unknown command
unknown command
unknown command
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值