题解 | #配置文件恢复# 简单粗暴

配置文件恢复

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;		
			}
		}
	}

}

全部评论

相关推荐

喜欢走神的孤勇者练习时长两年半:爱华,信华,等华,黑华
点赞 评论 收藏
分享
11-18 15:57
门头沟学院 Java
最终归宿是测开:这个重邮的大佬在重邮很有名的,他就喜欢打92的脸,越有人质疑他,他越觉得爽😂
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务