题解 | #配置文件恢复#

配置文件恢复

https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
bool isMatch(string str, string s) {
    for (int i = 0; i < s.size(); i++) {
        if (str[i] != s[i]) {
            return false;
        }
    }
    return true;
}
int main() {
    string input;
    vector<vector<string>> v;
    vector<string> s1 = {"reset"};
    vector<string> s2 = {"reset", "board", "board fault"};
    vector<string> s3 = {"board", "add", "where to add"};
    vector<string> s4 = {"board", "delete", "no board at all"};
    vector<string> s5 = {"reboot", "backplane", "impossible"};
    vector<string> s6 = {"backplane", "abort", "install first"};
    v.push_back(s1);
    v.push_back(s2);
    v.push_back(s3);
    v.push_back(s4);
    v.push_back(s5);
    v.push_back(s6);
    while (getline(cin, input)) { // 注意 while 处理多个 case
        vector<string> v_input;
        istringstream s(input);
        string temp;
        int count = 0;
        while (getline(s, temp, ' ')) {
            v_input.push_back(temp);
            count++;
        }
        if (count == 1) {
            if (isMatch(v[0][0], v_input[0])) {
                cout << "reset what" << endl;
            } else {
                cout << "unknown command" << endl;
            }
        } else {
            bool matched = false;
            int count = 0;
            int index;
            for (int i = 1; i < v.size(); i++) {
                if (isMatch(v[i][0], v_input[0]) && isMatch(v[i][1], v_input[1])) {
                    index = i;
                    count++;
                }
            }
            if (count == 1) {
                cout << v[index][2] << endl;
                matched = true;
            }
            if (matched == false) {
                cout << "unknown command" << endl;
            }
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务