题解 | #配置文件恢复#

配置文件恢复

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

#include <iostream>
using namespace std;
#include <string>
#include <map>
#include <sstream>

void func(string str) {
    if (str == " ") {
        cout << "reset what" << endl;
    } else if (str == "board") {
        cout << "board fault" << endl;
    } else if (str == "add") {
        cout << "where to add" << endl;
    } else if (str == "delete") {
        cout << "no board at all" << endl;
    } else if (str == "backplane") {
        cout << "impossible" << endl;
    } else if (str == "abort") {
        cout << "install first" << endl;
    }
}

int main() {
    multimap<string, string> od;
    od.insert(pair<string, string>("reset", " "));
    od.insert(pair<string, string>("reset", "board"));
    od.insert(pair<string, string>("board", "add"));
    od.insert(pair<string, string>("board", "delete"));
    od.insert(pair<string, string>("reboot", "backplane"));
    od.insert(pair<string, string>("backplane", "abort"));
    string rs = "reset";
    string str;
    int cnt;
    while (getline(cin, str)) {
        istringstream is(str);
        string str1, str2;
        getline(is, str1, ' ');
        getline(is, str2);
        cnt = 0;
        string ord;
        for (auto& l : od) {
            int x = l.first.find(str1);
            int y = l.second.find(str2);
            if (x != -1 && y != -1 && str2 != "" && l.first[0] == str1[0] && l.second[0] == str2[0] ) {
                cnt++;
                ord = l.second;
            } else if (x != -1 && l.second == " " && str2 == ""&& l.first[0] == str1[0]) {
                cnt++;
                ord = l.second;
            }
        }
        // cout << ord << cnt << endl;
        if (cnt == 1) {
            func(ord);
        } else {
            cout << "unknown command" << endl;
        }
    }
}

看起来简单但是超多坑的一题

模糊搜索只能从头匹配

要做好reset单指令的区分

华为机试刷题记录 文章被收录于专栏

记录一下手打代码的解题思路方便复习

全部评论

相关推荐

05-01 22:41
中南大学 Java
点赞 评论 收藏
分享
面试官问:为什么不考研?该怎么回答啊😭我说现在的就业环境差到底了,还有就是我不想学数学,感觉面试官笑容都凝固了😢
DayDayNoBug的鲜芋球:我说的是“上学期其实尝试过去探索一些研究的方向,但感觉那些对我来说都没有很大的吸引力,相比起研究我可能更喜欢开发这种实践性的东西,它会让我觉得很有意思并且会为之深入进去”(虽然也不知这个回答怎么样哈哈哈哈哈哈)
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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