题解 | #配置文件恢复# 简单粗暴
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
#include<string> #include<iostream> #include<unordered_map> #include<set> #include<vector> #include<sstream> using namespace std; int main() { vector<string>ml{"reset","reset board","board add","board delete","reboot backplane","backplane abort"}; vector<string>act{"reset what","board fault","where to add","no board at all","impossible","install first"}; unordered_map<string, string>ml_act; for (int i = 0; i < 6; i++) ml_act[ml[i]] = act[i]; unordered_map<string, set<string>>mlsl; mlsl["reset"] = set<string>{ "r","re","res","rese","reset" }; mlsl["board"] = set<string>{ "b","bo","boa","boar","board" }; mlsl["add"] = set<string>{ "a","ad","add" }; mlsl["delete"] = set<string>{ "d","de","del","dele","delet","delete" }; mlsl["reboot"] = set<string>{ "r","re","reb","rebo","reboo","reboot" }; mlsl["backplane"] = set<string>{ "b","ba","bac","back","backp","backpl","backpla","backplan","backplane" }; mlsl["abort"] = set<string>{ "a","ab","abo","abor","abort" }; vector<string>ml1{ "reset","board","reboot","backplane" }; vector<string>ml2{ "board","add","delete","backplane","abort" }; unordered_map<string, set<string>>ml1_2; ml1_2["reset"] = set<string>{ "board" }; ml1_2["board"] = set<string>{ "add","delete" }; ml1_2["reboot"] = set<string>{ "backplane" }; ml1_2["backplane"] = set<string>{ "abort" }; string line; vector<string>input; while (getline(cin, line)) { if (line == "") break; istringstream iss(line); input.clear(); string s; while (iss >> s) input.push_back(s); if (input.size() == 1) { string a = input[0]; if (mlsl["reset"].count(a)) cout << ml_act["reset"] << endl; else cout << "unknown command" << endl; } else { string a = input[0]; string b = input[1]; vector<string>s1; vector<string>s2; for (string m : ml1) if (mlsl[m].count(a)) s1.push_back(m); for (string m : ml2) if (mlsl[m].count(b)) s2.push_back(m); if (s1.size() == 0 || s2.size() == 0) { cout << "unknown command" << endl; } else { vector<string>s12; for (string m1 : s1) for (string m2 : s2) if (ml1_2[m1].count(m2)) s12.push_back(m1+" "+m2); if (s12.size() == 1) cout << ml_act[s12[0]] << endl; else cout << "unknown command" << endl; } } } }