题解 | #配置文件恢复#

配置文件恢复

http://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5

一次AC我是真的没想到啊,可恶。这道题就是有点弯弯绕绕,但你仔细分析发现它只是不停if else

#include <algorithm>
#include <sstream>
#include <map>

using namespace std;

int main() {
    string str;
    map<string,string> m1 = {//这里存一个字串的
        {"reset","reset what"}
    };
    map<string,string> m2 = {//这里存两个字串
        {"reset board","board fault"},
        {"board add","where to add"},
        {"board delete","no board at all"},
        {"reboot backplane","impossible"},
        {"backplane abort","install first"}
    };
    while(getline(cin,str)){
        bool havespace = false;//我首先判断它是不是有两个字符,有的话中间有空格
        string temp1,temp2;
        auto pos = str.find(' ');
        if(pos!=string::npos) havespace =true;
        if(havespace){
            istringstream ss;
            ss.str(str);
            getline(ss,temp1,' ');
            getline(ss,temp2);
        }
        else{
            temp1 = str;
        }
        if(!havespace){
            bool find =false;
            for(auto &it:m1){
                if(it.first.find(temp1)==0){
                    find = true;
                    cout<<m1[it.first]<<endl;
                    break;
                }
            }//for
            if(!find)cout<<"unknown command"<<endl;
        }//if
        else if(havespace){
            //int first =false,second = false;
            int match = 0;
            string out;
            
            for(auto &it:m2){
                
                if(it.first.find(temp1)==0){
                    auto pos = it.first.find(' ');
                    string temp = it.first.substr(pos+1);//后面的看匹配不匹配
                    if(temp.find(temp2)==0) {//其实题干思考一下逻辑就是先匹配
                    //空格前的在匹配空格后的,而且匹配的只能有一个,其它情况都是
                    //unknown
                        match++;
                        out = it.first;
                    }//if
                }//外层if
            }//for
            if(match==1)cout<<m2[out]<<endl;
            else cout<<"unknown command"<<endl;
        }
        
    }
}
全部评论
疯狂的if else,快写吐了都
点赞
送花
回复
分享
发布于 2022-08-26 22:59 天津

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务