题解 | #配置文件恢复#

配置文件恢复

https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5?tpId=37&tqId=21289&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

vector<string>  ori_ord = {"reset", "reset board", "board add", "board delete","reboot backplane", "backplane abort", "he he"};//命令表
void output(int i){//对应执行
    switch (i) {
        case 0: cout << "reset what" <<endl;break;
        case 1: cout << "board fault" <<endl;break;
        case 2: cout << "where to add" <<endl;break;
        case 3: cout << "no board at all" <<endl;break;
        case 4: cout << "impossible" <<endl;break;
        case 5: cout << "install first" <<endl;break;
        case 6: cout << "unknown command" <<endl;break;
    }
    return;
}
int main() {
    string key_o;
    while (getline(cin, key_o)) { // 注意 while 处理多个 case
        int space = key_o.find(' ');
        int len = key_o.size();
        int fo;
        //bool is = false;
        if(space==string::npos){//只有一个字串
            if(ori_ord[1].find(key_o)==0)
                fo = 0;
            else
                fo = 6;
            //cout << fo;//检测代码块输出
        }else{
            string k1,k2;
            k1 = key_o.substr(0, space);
            k2 = key_o.substr(space+1, len-space);
            int rep = 0;
            fo = 6;
            for(int i = 1; i < 6; i++){
                int o_sp=ori_ord[i].find(' ')+1;//查找出空格的后一位为第二个单字串开头
                //cout<<ori_ord[1][o_sp];//ori_ord[1].find(k2, o_sp);
                if(ori_ord[i].find(k1, 0) == 0 && ori_ord[i].find(k2, o_sp)==o_sp){
                    //若两个从字串开头的查找返回的是字串起点,说明命令匹配成功
                    fo = i;
                    rep++;//查找成功的次数,一次以上指令不唯一
                    //cout << fo <<rep <<endl;
                }
            }
            if(rep > 1)
                fo = 6;
            //cout << fo;
        }
        output(fo); 


                
        //cout << space <<endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-29 17:30
找实习找着找着就要进入7月了,马上秋招也要开始了,找实习还有意义吗?
绝迹的星:有面就面, 没面上就当日薪4位数大佬免费培训, 面上了再考虑要不要实习
点赞 评论 收藏
分享
frutiger:逆天,我家就安阳的,这hr咋能说3k的,你送外卖不比这工资高得多?还说大厂来的6k,打发叫花子的呢?这hr是怎么做到说昧良心的话的
找工作时遇到的神仙HR
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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