华为OJ——配置文件恢复

配置文件恢复

题目描述

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、若匹配失败,打印“unkonw 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>
#include<cstring>
#include<algorithm>
#include<map>
#include<fstream>
using namespace std;

map<string,string> command;

bool match(string s1,string s2)
{
    if(s1.length() > s2.length())
        return false;
    for(int i=0; i<s1.length(); i++)
        if(s1[i]!=s2[i])
            return false;
    return true;
}

string MatchCommand(string str)
{
    string result;
    map<string,string>::iterator it;
    if(str.find(' ')==string::npos)//只有一条命令
    {
        if(match(str,"reset"))
            result="reset what";
        else
            result="unkown command";
    }
    else
    {
        int count=0;
        //统计单词个数
        for(int i=0; i<str.length(); i++)
        {
            if(str[i]==' ')
                count++;
        }
        count++;
        if(count>2)//大于两个单词
            result="unkown command";
        else//含有2个单词
        {
            int pos=str.find(' ');
            string s1=str.substr(0,pos);
            string s2=str.substr(pos+1,str.length());
            int counter=0;
            for(it=command.begin(); it!=command.end(); it++)
            {
                string ss=it->first;
                int index=ss.find(' ');
                string str1=ss.substr(0,index);
                string str2=ss.substr(index+1,ss.length());
                if(match(s1,str1) &&  match(s2,str2))
                {
                    counter++;
                    result=it->second;
                }
            }
            if(counter!=1)
                result="unkown command";
        }
    }

    return result;
}

int main()
{

    //freopen("1.txt","r",stdin);
    //构造map映射
    command.insert(pair<string,string>("reset board", "board fault"));
    command.insert(pair<string,string>("board add", "where to add"));
    command.insert(pair<string,string>("board delet", "no board at all"));
    command.insert(pair<string,string>("reboot backplane", "impossible"));
    command.insert(pair<string,string>("backplane abort", "install first"));

    string str;
    while(getline(cin,str))
    {
        string result=MatchCommand(str);
        cout<<result<<endl;
    }

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值