题解 | #配置文件恢复#
配置文件恢复
http://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<pair<string, string>> instru = { { "reset","" }
,{ "reset","board" }
,{ "board","add" }
,{ "board","delete" }
,{ "reboot","backplane" }
,{ "backplane","abort" } };
vector<string> outInsru = { "reset what" ,"board fault" ,"where to add" ,"no board at all" ,"impossible" ,"install first" };
string s;
while (getline(cin,s))
{
string s1 = "", s2 = "";
int start = 0;
while (start < s.size() && s[start] != ' ')//分割字符串
start++;
s1 = s.substr(0, start);
if (start != s.size())s2 = s.substr(start + 1, s.size());
int count1 = 0, count2 = 0;
string result;
for (auto iter=instru.begin();iter!=instru.end();iter++)
{
int i1=iter->first.find(s1);
int i2;
if (s2 != "") {//判断是否有第二关键字
i2 = iter->second.find(s2);
}
else if(s2==""&&iter->second==""){
i2 = 0;
}
else i2 = -1;
if (i1 == 0 && i2 == 0)//判断关键字是否唯一
{
count1++;
count2++;
result = outInsru[iter - instru.begin()];
}
}
if (count1 == 1 && count2 == 1)//判断是否唯一或者是否存在
{
cout << result << endl;
}
else {
cout << "unknown command" << endl;
}
}
return 0;
}
查看12道真题和解析
