题解 | #配置文件恢复#

配置文件恢复

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

//双字串输入拆分+双命令拆分+vector容器,简单直接

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
    string str;
    string sigle = "reset";
    vector<vector<string>> tp1{{"reset","board","board fault"},{"board","add","where to add"},
                               {"board","delete","no board at all"},{"reboot","backplane","impossible"},
                               {"backplane","abort","install first"}};
    while(getline(cin, str)){
        int n = count(str.begin(), str.end(), ' ');
        if(n == 0){
            if(sigle.find(str) == 0){
                cout << "reset what" <<endl;
            }else{
                cout << "unknown command" <<endl;
            }
        }else{
            int l = str.find(' ');
            string str1 = str.substr(0, l);
            string str2 = str.substr(l+1, str.length() -l - 1);
            int count = 0;
            string ans;
            for(int i = 0 ; i < 5; i ++ ){
                if(tp1[i][0].find(str1) == 0 && tp1[i][1].find(str2) == 0){
                    count ++ ;
                    ans = tp1[i][2];
                }
            }
            if(count == 1){
                cout << ans <<endl;
            }else{
                cout << "unknown command" <<endl;
            }
        }
    }
    return 0;
}

全部评论
强,写的泰裤辣!
1 回复 分享
发布于 2023-04-22 01:50 福建
楼主我觉着有一点点不严谨,判断a b是否匹配是看a是不是b的前缀而不是a是不是b的子串
点赞 回复 分享
发布于 2022-08-26 22:58 天津

相关推荐

评论
19
2
分享
牛客网
牛客企业服务