题解 | #配置文件恢复#

配置文件恢复

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

借助这道题提醒要看一眼prime

复习一下istringstream的用法

这个函数很好用

#include <iostream>
#include <bits/stdc++.h>
#include <sstream>
#include <vector>
using namespace std;
vector<string> order({"reset","reset board","board add","board delete","reboot backplane","backplane abort"});
vector<string> doworkk({"reset what","board fault","where to add","no board at all","impossible","install first"});
bool compare(string src,string od);
void process(string od1);
void process(string od1,string od2);
int main() {
    string str;
    while(getline(cin,str)){
        string od1,od2;
        istringstream record(str);
        record>>od1;
        if(str!=od1){
            record>>od2;
        }
        if(od2==""){
            process(od1);
        }
        else{
            process(od1,od2);
        }

    }
}
void process(string od1){
    vector<int>ans;
    for(int i = 0;i<1;i++){
        if(compare(order[i],od1)){
            ans.push_back(i);
        }
    }
    if(ans.size()!=1){
        cout<<"unknown command"<<endl;
    }
    else{
        cout<<doworkk[ans[0]]<<endl;
    }
}
void process(string od1,string od2){
    vector<int>ans;
    for(int i =1;i<6;i++){
        string str = order[i];
        istringstream record(str);//拆解为两个单词
        string str1,str2;
        record>>str1>>str2;
        if(compare(str1,od1)&&compare(str2,od2)){//分别进行比对,全是true才添加下标
            ans.push_back(i);
        }
    }
    if(ans.size()!=1){
        cout<<"unknown command"<<endl;
    }
    else{
        cout<<doworkk[ans[0]]<<endl;
    }
}
bool compare(string src,string od){//比较函数
    int odlen = od.size();
    int srclen = src.size();
    if(odlen>srclen) return false;//长度大于src直接false
    int index = 0;
    while(odlen--){
        if(od[index]!=src[index]){//只要有一个不同直接false
            return false;
        }
        index++;
    }
  	//较短的od和长的src的前odlen位都相同
    return true;
}

// 64 位输出请用 printf("%lld")

全部评论

相关推荐

牛客鼠:校友你这简历基本无敌了,春招刷刷题去冲大厂
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务