题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main() { string inp; vector<vector<string>> coms(5); coms[0] = {"reset", "board", "board fault"}; coms[1] = {"board", "add", "where to add"}; coms[2] = {"board", "delete", "no board at all"}; coms[3] = {"reboot", "backplane", "impossible"}; coms[4] = {"backplane", "abort", "install first"}; while (getline(cin, inp)) { // 注意 while 处理多个 case int spacePos = inp.find(' '); if (spacePos == string::npos) { string command = "reset"; if (command.find(inp) == 0) { cout << "reset what" << endl; } else { cout << "unknown command" << endl; } } else { string inp1 = inp.substr(0, spacePos); string inp2 = inp.substr(spacePos + 1, inp.size() - spacePos - 1); string result; int matchCnt = 0; for (int i = 0; i < 5; ++i) { if (coms[i][0].find(inp1) == 0 && coms[i][1].find(inp2) == 0) { result = coms[i][2]; ++matchCnt; } } if (matchCnt == 1) cout << result << endl; else cout << "unknown command" << endl; } } } // 64 位输出请用 printf("%lld")