CCF CSP 201612-3权限查询
按题意模拟,一层层嵌套,有注释
#include
#include
#include
#include
using namespace std;
mapcategory,cy;
map >role;
map >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 mp = role[s];
for(map::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 > ::iterator it = user.begin(); it != user.end(); it ){
//cout<first<
//for(map::iterator it1 = it->second.begin(); it1 != it->second.end(); it1 ){
//cout<first<second<
//}
//cout<
//}
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<
}
else{
cout<
}
}
else{
cout<
}
}
else{
if(user[name].count(privilege)){
if(user[name][privilege] == -1)
cout<
else{
cout<
}
}
else{
cout<
}
}
}
else{
cout<
}
cout<
}
return 0;
}
来源:https://www.icode9.com/content-1-767851.html