题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<vector<string>> command = {{"reset"}, {"reset", "board"}, {"board", "add"}, {"board", "delete"}, {"reboot", "backplane"}, {"backplane", "abort"}};
string s;
while (getline(cin, s)) {
int n = s.size(), blank_loc = -1, num = 1, out = 0;
for (int i = 0; i < n; i++) {
if (s[i] == ' ') {
blank_loc = i;
break;
}
}
// 判断本次输入几个字符串num
if (blank_loc != -1) {
num = 2;
}
if (num == 1) {
if (n <= command[0][0].size() && s == command[0][0].substr(0, n)) {
out = 1;
}
} else if (num == 2) {
int tmp = 0;
int n1 = blank_loc, n2 = n - blank_loc - 1;
for (int i = 1; i <= 5; i++) {
if (n1 <= command[i][0].size() &&
s.substr(0, n1) == command[i][0].substr(0, n1)) {
if (n2 <= command[i][1].size() &&
s.substr(blank_loc + 1, n2) == command[i][1].substr(0, n2)) {
tmp++;
out = i + 1;
}
}
}
if (tmp != 1) {
out = 0;
}
}
switch (out) {
case 0:
cout << "unknown command" << endl;
break;
case 1:
cout << "reset what" << endl;
break;
case 2:
cout << "board fault" << endl;
break;
case 3:
cout << "where to add" << endl;
break;
case 4:
cout << "no board at all" << endl;
break;
case 5:
cout << "impossible" << endl;
break;
case 6:
cout << "install first" << endl;
break;
}
}
return 0;
}
// 64 位输出请用 printf("%lld")
本题难点在于如何完成判断匹配的逻辑,主要通过判断长度和是否相等完成匹配,通过计数完成唯一。
美的集团公司福利 724人发布


