题解 | #配置文件恢复#

配置文件恢复

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

#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
const vector<string> S{
    "reset"
};
const vector<string> S_{
    "reset board",
    "board add",
    "board delete",
    "reboot backplane",
    "backplane abort"
};
map<string, string> umap{
    {"reset", "reset what"},
    {"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"},
    {"other", "unknown command"}
};
string find_(string str, vector<string> V) {
    int num = 0;
    string tmp = "other";
    for (auto it : V) {
        if (it.find(str) == 0) {
            num++;
            tmp = it;
            if (num > 1) {
                tmp = "other";
                break;
            }
        }
    }
    return tmp;
}
int main() {
    string str;
    while (getline(cin, str)) {
        //无空格
        if (str.find(' ') == string::npos) {
            cout << umap[find_(str, S)] << endl;
        }//有空格
        else {
            string command_1 = str.substr(0, str.find(' '));
            vector<string> vec;
            for (auto it : S_) {
                string it_ = it.substr(0, it.find(' '));
                if (it_.find(command_1) == 0)    vec.push_back(it);
            }
            string command_2 = str.substr(str.find(' ') + 1);
            int num = 0;
            string result = "other";
            for (auto it : vec) {
                string it_ = it.substr(it.find(' ') + 1);
                if (it_.find(command_2) == 0) {
                    num++;
                    result = it;
                    if (num > 1) {
                        result = "other";
                        break;
                    }
                }
            }
            cout<< umap[result]<<endl;
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

03-10 14:19
已编辑
重庆邮电大学 前端工程师
球Offer上岸👑:测试也难求一面 逆天
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务