题解 | #配置文件恢复#

配置文件恢复

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")

全部评论

相关推荐

求offer的大角牛:不吃香菜
点赞 评论 收藏
分享
05-20 21:57
已编辑
门头沟学院 Java
喜欢吃卤蛋的悲伤蛙在...:建信融通没消息吧,我2说有实习挂简历不理了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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