CCF CSP 201612-3权限查询
按题意模拟,一层层嵌套,有注释
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
map<string,int>category,cy;
map<string,map<string, int> >role;
map<string,map<string, int> >user;
int main(){
int n;
cin>>n;
//存储权限
for(int i = 0; i < n; i++){
string s;
cin>>s;
int f = -1;
//分有权限和无权限两种
if((f = s.find(':'))!=-1){
int num = 0;
for(int j = f+1; j < s.size(); j++){
num = num * 10 + s[j]-'0';
}
category[s.substr(0,f)] = num;
}
else category[s] = -1;
}
cin>>n;
//存储权限
for(int i = 0; i < n; i++){
string str,s;
int m;
cin>>str>>m;
cy.clear();
for(int k = 0; k < m; k++){
cin>>s;
int f = -1;
//带权限的存储最大权限,无权限的存储为-1
if((f = s.find(':'))!=-1){
int num = 0;
for(int j = f+1; j < s.size(); j++){
num = num * 10 + s[j]-'0';
}
if(cy.count(s.substr(0,f))){
cy[s.substr(0,f)] = max(cy[s.substr(0,f)],num);
}
else{
cy[s.substr(0,f)] = num;
}
}
else cy[s] = -1;
}
role[str] = cy;
}
//存储角色
cin>>n;
for(int i = 0; i < n; i++){
string str,s;
int m;
cin>>str>>m;
cy.clear();
for(int k = 0; k < m; k++){
cin>>s;
int f = -1;
//遍历角色中的权限,替换到用户中
map<string,int> mp = role[s];
for(map<string,int>::iterator it = mp.begin(); it != mp.end(); it++){
if(cy.count(it->first)){
cy[it->first] = max(cy[it->first],it->second);
}
else{
cy[it->first] = it->second;
}
}
}
user[str] = cy;
}
// for(map<string,map<string,int> > ::iterator it = user.begin(); it != user.end(); it++){
// cout<<it->first<<":";
// for(map<string,int>::iterator it1 = it->second.begin(); it1 != it->second.end(); it1++){
// cout<<it1->first<<" "<<it1->second<<" ";
// }
// cout<<endl;
// }
cin>>n;
for(int i = 0; i < n; i++){
string name,privilege;
cin>>name>>privilege;
//是否有该用户
if(user.count(name)){
int f = -1;
//是否是带权限查询
if((f = privilege.find(':'))!=-1){
int num = 0;
for(int j = f+1; j < privilege.size(); j++){
num = num * 10 + privilege[j]-'0';
}
//权限是否存在
if(user[name].count(privilege.substr(0,f))){
if(user[name][privilege.substr(0,f)] >= num){
cout<<"true";
}
else{
cout<<"false";
}
}
else{
cout<<"false";
}
}
else{
if(user[name].count(privilege)){
if(user[name][privilege] == -1)
cout<<"true";
else{
cout<<user[name][privilege];
}
}
else{
cout<<"false";
}
}
}
else{
cout<<"false";
}
cout<<endl;
}
return 0;
}