题解 | #配置文件恢复#
配置文件恢复
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")