题解 | #配置文件恢复#

配置文件恢复

https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5?tpId=37&tqId=21289&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26pageSize%3D50%26search%3D%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=

#include <iostream>
#include <locale>
#include <map>
#include <string>
#include <vector>
using namespace std;

// 匹配规则
map<vector<string>, string> rule = {
    {{"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"}
};

// keys 的值
vector<vector<string>> keys = {
    {"reset"},
    {"reset","board"},
    {"board","add"},
    {"board","delete"},
    {"reboot","backplane"},
    {"backplane","abort"}
};

// 实现mysplit()
vector<string> mySplit(string com){
    int left = 0;
    int right = 1;
    vector<string> ans;

    while(right < com.size()){
        if(com.at(right) == ' '){
            ans.push_back(com.substr(left, right - left));
            left = right + 1;
            right = left;
        }
        right += 1;
    }
    ans.push_back(com.substr(left, right - left));
    return ans;
}

// 根据com_list找到对应的value
string findCom(vector<string> com_list){
    int ans = 0;
    string com = "";
    // cout << com_list[0] << " " << com_list[1] << endl;
    
    for(vector<string> key : keys){
        
        if(key.size() < 2){
            continue;
        }
        // cout << key.at(0) << " " << key.at(1) << endl;
        if(key.at(0).substr(0, com_list.at(0).size()) == com_list.at(0)
            && 
            key.at(1).substr(0, com_list.at(1).size()) == com_list.at(1))
        {
            ans++;
            com = rule[key];  
        }
        
    }

    return (ans == 1) ? com : "unknown command";
}

// 通过 com 构造 comlist
string match(string com){
    vector<string> com_list = mySplit(com);
    // cout << com_list.size() << endl;

    if(com_list.size() == 1){
        if(keys[0][0].substr(0, com_list.at(0).size()) == com_list.at(0)){
            return "reset what";
        }else{
            return "unknown command";
        }
    }else if(com_list.size() == 2){
        return findCom(com_list);
    }
    return "unknown command";
}

int main() {
    string a;
    // cout << match("backplane abort");
    // cout << match("b addr");
    while (getline(cin, a)) { // 注意 while 处理多个 case
        // cout << a << endl;
        cout << match(a) << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

七牛云头号黑子:人家是过度包装被看出来没过简历,你是包都不包啊兄弟
点赞 评论 收藏
分享
02-14 12:40
门头沟学院 Java
程序员花海:1.面试要求必须Java笔试不一定 2.难度对等秋招 远超于日常实习是因为同一批次且转正很多 竞争压力大 3.第一个加点指标,上线了就把接口性能加上去 使用本地缓存这个不算亮点 只是技术选型,要把为什么采用这个和背后的思考写出来而不是单纯堆叠技术没意义 4.八股要一直看 很容易忘记 5.拼团交易这个老问题 堆积技术 另外建议你把奖项合并到教育背景 没必要拆出来放最后
我的简历长这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务