[编程题]配置文件恢复

来源:互联网 发布:重庆seo推荐盛庆策动 编辑:程序博客网 时间:2024/09/12 17:39

Talk is cheap, show me the code.

一、问题描述

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

reset reset whatreset board board faultboard add where to addboard delet no board at allreboot backplane impossiblebackplane abort install firsthe 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”

输入描述:

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

输出描述:

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

输入例子:

resetreset boardboard addboard deletreboot backplanebackplane abort

输出例子:

reset whatboard faultwhere to addno board at allimpossibleinstall 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 bre bb a

输出:

unknown commandunknown commandunknown command
0 0