#屎山代码#配置文件恢复#时空复杂度o(n)/o(1)
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
#include <unordered_map> #include <iostream> #include <vector> using namespace std; unordered_map<string,int> reset; unordered_map<string,int> board; unordered_map<string,int> reboot; unordered_map<string,int> backplane; unordered_map<string,int> add; unordered_map<string,int> Delete; unordered_map<string,int> Abort; vector<string> split(string order){ vector<string> order_list; if(order[0]!='r'&&order[0]!='b') return order_list; string temp; int length = order.length(); for(int i=0;i<length;i++){ if(order[i]!=' ') temp.push_back(order[i]); else{ if(i==length-1||(order[i+1]!='b'&&order[i+1]!='a'&&order[i+1]!='d')){ order_list.clear(); return order_list; } else{ order_list.push_back(temp); temp.clear(); } } } order_list.push_back(temp); return order_list; } string process(string order){ vector<string> order_list = split(order); if(order_list.size()==1){ string s1 = order_list[0]; if(reset.find(s1)!=reset.end()) return "reset what"; else return "unknown command"; } else if(order_list.size()==2){ string s1 = order_list[0],s2 = order_list[1]; if(board.find(s1)!=board.end()&&Delete.find(s2)!=Delete.end()) return "no board at all"; else if(reset.find(s1)!=reset.end()&&board.find(s2)!=board.end()){ if(reboot.find(s1)==reboot.end()||(reboot.find(s1)!=reboot.end()&&backplane.find(s2)==backplane.end())) return "board fault"; else return "unknown command"; } else if(board.find(s1)!=board.end()&&add.find(s1)!=add.find(s2)){ if(backplane.find(s1)==backplane.end()||(backplane.find(s1)!=backplane.end()&&Abort.find(s2)==Abort.end())) return "where to add"; else return "unknown command"; } else if(reboot.find(s1)!=reboot.end()&&backplane.find(s2)!=backplane.end()){ if(reset.find(s1)==reset.end()||(reset.find(s1)!=reset.end()&&board.find(s2)==board.end())) return "impossible"; else return "unknown command"; } else if(backplane.find(s1)!=backplane.end()&&Abort.find(s2)!=Abort.end()){ if(board.find(s1)==board.end()||(board.find(s1)!=board.end()&&add.find(s2)==add.end())) return "install first"; else return "unknown command"; } else return "unknown command"; } else{ return "unknown command"; } } int main() { string order; string s1="reset",s2="board",s3="reboot",s4="backplane",s5="add",s6="delete",s7="abort"; for(int i=1;i<=5;i++){reset.insert(make_pair(s1.substr(0,i),1));} for(int i=1;i<=5;i++){board.insert(make_pair(s2.substr(0,i),1));} for(int i=1;i<=6;i++){reboot.insert(make_pair(s3.substr(0,i),1));} for(int i=1;i<=9;i++){backplane.insert(make_pair(s4.substr(0,i),1));} for(int i=1;i<=3;i++){add.insert(make_pair(s5.substr(0,i),1));} for(int i=1;i<=6;i++){Delete.insert(make_pair(s6.substr(0,i),1));} for(int i=1;i<=5;i++){Abort.insert(make_pair(s7.substr(0,i),1));} while (getline(cin,order)) { cout<<process(order)<<endl; } return 0; }