配置文件恢复
配置文件恢复_牛客网
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5?tpId=37&tqId=21289&rp=0&ru=/ta/huawei&qru=/ta/huawei/question-ranking
import sys oneKey='reset' twoKeys=['reset board','board add','board delet','reboot backplane','backplane abort'] do=['board fault','where to add','no board at all','impossible','install first'] def do_command(order,twoKeys,do): count=0 index=0 for k in twoKeys:#匹配第一关键字和第二关键字 if order[0]==k.split()[0][:len(order[0])] and order[1]==k.split()[1][:len(order[1])]: count+=1#对符合匹配结果的情形计数,因为匹配结果唯一才算匹配成功 index=twoKeys.index(k) if count>1 or count==0: print('unkown command') else: print(do[index]) for i in sys.stdin:#按行读取命令 order=i.strip().split()#对每一个命令切片 if len(order)<=0 or len(order)>2:#判断命令是几个字符串 print('unkown command') elif len(order)==1:#当命令是一个字符串,判断该字符串与‘reset’是否匹配 if order[0] == oneKey[:len(order[0])]:#此处要特别注意用order[0] print('reset what') else: print('unkown command') elif len(order)==2: do_command(order,twoKeys,do) ''' while True: try: for i in sys.stdin: order=i.strip().split() if len(order)<=0 or len(order)>2: print('unkown command') elif len(order)==1: if order[0] == oneKey[:len(order[0])]: print('reset what') else: print('unkown command') elif len(order)==2: do_command(order,twoKeys,do) except: break '''
#include <iostream> #include <string> #include <sstream> using namespace std; bool match(string str,string s){ return str.find(s)==0;//判断str.find(s)返回的位置是否是第一位 } int main(){ string s; string Keys[6]={"reset","reset board","board add","board delet","reboot backplane","backplane abort"}; string Do[7]={"reset what","board fault","where to add","no board at all","impossible","install first","unkown command"}; while(getline(cin,s)){ string s1,s2,Keys1,Keys2; int index,temp,count=0; stringstream ss(s);//读取s中的单字,比如hello world ,就会读取hello和world ss >> s1 >> s2;//C++这个字符串流很棒,这里和python的split()很像 if(s2.empty()) index=match(Keys[0],s1) ? 0:6; else{ for(int k=1;k<6;k++){ stringstream sss(Keys[k]); sss >> Keys1 >> Keys2; if(match(Keys1,s1) && match(Keys2,s2)){ count++; temp=k; } } if(count>1 || count==0) index=6; else index=temp; } /* if(ss<=0 || ss.size()>2) index=6; else if(ss.size()==1){ if(s1==Keys[0][:s1.size()]) index=0; else index=6; } else{ for(int k=0;k<6;k++){ stringstream sss(Keys[k]); sss >> Keys[k]_1 >> Keys[k]_2; if((s1==Keys[k]_1[:s1.size()]) && (s2==Keys[k]_2[:s2.size()])){ count++; index=k; } } if(count>1 || count==0) index=6; */ cout << Do[index] << endl; } return 0; }