题解 | #配置文件恢复#
配置文件恢复
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单指令的区分
华为机试刷题记录 文章被收录于专栏
记录一下手打代码的解题思路方便复习