AcWing 3239. 权限查询-csp9(3)
原题链接
中等
作者:
YAX_AC
,
2024-11-19 13:51:02
,
所有人可见
,
阅读 3
/*
先看用户存不存在:
不存在,false
存在:
看查询的权限是否有等级:
(1)有等级:
该用户没有该权限或val>mx_val,false
查询有等级,true
查询的没有等级,数字(max_val)
(2)没有等级:
该用户没有该权限,false
*/
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<string,int> PSI;
int P,R,U,Q;
map<string,int> power;//-1不分等级,否则分等级
map<string,vector<PSI>> role;
map<string,vector<string>> user;
PSI decode(string s)
{
int p = -1;
for(int i = 0; i<(int)s.size()+1; i++)
if(s[i] == ':')
{
p = i;
break;
}
if(p == -1) return (PSI){s,-1};//不分等级
string a = s.substr(0,p);
int b = stoi(s.substr(p+1));//ctoi(s.substr(p+1).c_str())
return (PSI){a,b};
}
string query(string User,string pw)
{
if(!user.count(User)) return "false";
PSI t = decode(pw);
//如果没有权限,则为false
bool have = false;
int mx_level = -1;
for(auto r:user[User])
for(auto p:role[r])
if(t.fi == p.fi)
{
mx_level = max(mx_level,p.se);
have = true;
}
if(have == false) return "false";
int val = t.se;
if(power[t.fi] != -1)
{
if(have == false || val>mx_level) return "false";
if(t.se != -1) return "true";
return to_string(mx_level);
}
else
{
if(have == false) return "false";
return "true";
}
}
int main()
{
cin>>P;
for(int i = 0; i<P; i++)
{
string s; cin>>s;
PSI t = decode(s);
power[t.fi] = t.se;
}
cin>>R;
for(int i = 0; i<R; i++)
{
string Role; cin>>Role;
int count; cin>>count;
while(count--)
{
string s;
cin>>s;
role[Role].push_back(decode(s));
}
}
cin>>U;
for(int i = 0; i<U; i++)
{
string User; cin>>User;
int count; cin>>count;
while(count--)
{
string Role; cin>>Role;
user[User].push_back(Role);
}
}
cin>>Q;
while(Q--)
{
string User; cin>>User;
string pw; cin>>pw;
cout<<query(User,pw)<<endl;
}
return 0;
}