分等級權限的不帶等級查詢:若是權限自己分等級,查詢不帶等級,則返回具備該類權限的等級;若是不具備該類的任何等級權限,則返回「否」。c++
對於不分等級權限,只能查詢該用戶是否具備該權限,查詢中不能指定等級。git
若是用戶不存在,或者查詢的權限沒有定義,則應該返回 false。數據結構
3
crm:2
git:3
game
4
hr 1 crm:2
it 3 crm:1 git:1 game
dev 2 git:3 game
qa 1 git:2
3
alice 1 hr
bob 2 it qa
charlie 1 dev
9
alice game
alice crm:2
alice git:0
bob git
bob poweroff
charlie game
charlie crm
charlie git:3
malice game測試
false
true
false
2
false
true
false
true
falsespa
#include <bits/stdc++.h> using namespace std; int p,r,u,q; class Role; class User; map<string,int>hasLevRight; map<string,int>noLevRight; map<string,Role>roles; map<string,User>users; typedef pair<string,int>P; class Role { public: string roleName; map<string,int>levedRight; map<string,int>unlevedRight; }; class User { public: string userName; map<string,int>levedRight; map<string,int>unlevedRight; }; int stringToInt(string s) { int num=0; for(int i=0;i<s.size();i++) num = num*10+s[i]-'0'; return num; } P processRIght(string s) { string first,second; int i=0; for(;i<s.size()&&s[i]!=':';i++) first+=s[i]; for(i++;i<s.size();i++) second+=s[i]; if(second != "") return P(first,stringToInt(second)); else return P(first,-1); } // 未測試 void combine(string name,string roleName,User& user) { //cout<<name<<" "<<roleName<<endl; Role role = roles[roleName]; //cout<<role.roleName<<" "; map<string,int>::iterator iter; for(iter = role.levedRight.begin();iter!=role.levedRight.end();iter++) { string rightName = iter->first; int lev = iter->second; user.levedRight[rightName] = max(lev,user.levedRight[rightName]); } for(iter = role.unlevedRight.begin();iter!=role.unlevedRight.end();iter++) { string rightName = iter->first; user.unlevedRight[rightName]=-1; } } void input1() { string s,first,second; cin>>p; getchar(); while(p--){ getline(cin,s ); P all = processRIght(s); string first = all.first; int lev = all.second; if(lev==-1) noLevRight[first]=-1; else hasLevRight[first]=lev; } } void input2() { string s,name; int num; cin>>r; while(r--) { Role role; cin>>name>>num; role.roleName = name; while(num--) { cin>>s; P all = processRIght(s); string first = all.first; int lev = all.second; if(lev==-1) role.unlevedRight[first]=-1; else role.levedRight[first] = max(lev,role.levedRight[first]); roles[name] = role; } } } void input3() { string s,name; int num; cin>>u; while(u--) { cin>>name>>num; User user; user.userName = name; while(num--) { cin>>s; combine(name,s,user); } users[name]=user; } } void input4() { string userName,s; cin>>q; while(q--) { cin>>userName>>s; if(!users.count(userName)){ //用戶不存在 cout<<"false"<<endl; continue; } User user = users[userName]; P all = processRIght(s); string first = all.first; int lev = all.second; // 權限不存在 if(!hasLevRight.count(first)&&!noLevRight.count(first)){ cout<<"false"<<endl; continue; } if(noLevRight.count(first)) // un Leved { if(user.unlevedRight.count(first)) cout<<"true"<<endl; else cout<<"false"<<endl; } else if(hasLevRight.count(first)){ //leved Right if(!user.levedRight.count(first)){ cout<<"false"<<endl; } else { if(lev==-1) cout<<user.levedRight[first]<<endl; else { if(user.levedRight[first]>=lev) cout<<"true"<<endl; else cout<<"false"<<endl; } } } } } // 輸出全部內存中的數據,測試 void output() { // 全部的權限 map<string,int>::iterator iter; for(iter = hasLevRight.begin();iter!=hasLevRight.end();iter++) { cout<<iter->first<<" "<<iter->second<<endl; } for(iter = noLevRight.begin();iter!=noLevRight.end();iter++) cout<<iter->first<<" "<<iter->second<<endl; cout<<"----------------------------------"<<endl; // 每一個角色的權限 map<string,Role>::iterator iter2; for(iter2 = roles.begin();iter2!=roles.end();iter2++) { Role role = iter2->second; cout<<role.roleName<<":"<<endl; for(iter = role.levedRight.begin();iter!=role.levedRight.end();iter++) cout<<iter->first<<" "<<iter->second<<" "; cout<<endl; for(iter = role.unlevedRight.begin();iter!=role.unlevedRight.end();iter++) cout<<iter->first<<" "; cout<<endl; } cout<<"-------------------------------------"<<endl; // 每一個用戶的權限 map<string,User>::iterator iter3; for(iter3 = users.begin();iter3!=users.end();iter3++) { User user = iter3->second; cout<<user.userName<<" :"<<endl; for(iter = user.levedRight.begin();iter!=user.levedRight.end();iter++) cout<<iter->first<<" "<<iter->second<<" "; cout<<endl; for(iter = user.unlevedRight.begin();iter!=user.unlevedRight.end();iter++) cout<<iter->first<<" "; cout<<endl; } } int main() { //freopen("I:\\new 1.txt","r",stdin); input1(); input2(); input3(); input4(); //output(); return 0; }