CSP201612-3权限查询



多年后再回头看这道题觉得很简单,写起来还是很复杂,我的书写习惯不好,找bug找了很久。
特别注意在构建角色时,一个角色可能会有多个权限,取最大值,又要与无权限的-1区别对待。
#include<bits/stdc++.h>
#define N 10001
#define testa
using namespace std;
int n,temp,x,y,m,mmm;
int p,r,u,q;
string s,ss,sss;
long long ans=2000000000;
short mapp[N][N];
int in[N],out[N];
vector<int> l;
unordered_map<string,int> privilege;
class role{
public:
string name;
unordered_map<string,int> myp;
role(string na,vector<string> v){
name=na;
myp.clear();
for(auto k:v){
//cout<<k<<endl;
int nnnn=k.find(':');
//cout<<nnnn<<endl;
if(nnnn==k.npos){
sss=k;
mmm=-1;
}else{
mmm=atoi(k.substr(nnnn+1).c_str());
sss=k.substr(0,nnnn);
}
//cout<<"level "<<mmm<<endl;
if(mmm==-1){
this->myp[sss]=mmm;
}else this->myp[sss]=max(this->myp[sss],mmm);//important
}
#ifdef test
for(auto qwer:this->myp){
cout<<"in the role "<<na<<" "<<qwer.first<<" "<<qwer.second<<endl;
}
#endif
}
//role(){}
};
//role newrole;
vector<role>rolelist;
unordered_map<string,int>rolemap;
class user{
public:
string name;
unordered_map<string,int> myuserp;
user(string na,vector<string> v){
name=na;
for(auto k:v){
//cout<<k<<endl;
int roleid=rolemap[k];
role r=rolelist[roleid];
for(auto qwe:r.myp){
if(myuserp.count(qwe.first)!=0){
myuserp[qwe.first]=max(myuserp[qwe.first],qwe.second);
#ifdef test
cout<<na<<" get a new p named "<<qwe.first<<" level "<<qwe.second<<" from role "<<r.name<<endl;
cout<<"this is update\n";
cout<<roleid<<endl;
#endif
}else {
myuserp[qwe.first] = qwe.second;//???
#ifdef test
cout<<na<<" get a new p named "<<qwe.first<<" level "<<qwe.second<<" from role "<<r.name<<endl;
cout<<roleid<<endl;
#endif
}
}
}
}
//role(){}
};
vector<user> userlist;
unordered_map<string,int>usermap;
int main(){
cin>>p;
for(int i=0;i<p;i++){
cin>>s;
n=s.find(':');
if(n==s.npos){
y=-1;
}else{
y=atoi(s.substr(n+1).c_str());
s=s.substr(0,n);
}
privilege[s]=y;
}
cin>>r;
for(int i=0;i<r;i++){
cin>>s;
cin>>temp;
vector<string> q;
for(int jj=0;jj<temp;jj++){
cin>>ss;
q.push_back(ss);
}
role newrole=role(s,q);
rolelist.push_back(newrole);
rolemap[s]=rolelist.size()-1;
#ifdef test
//cout<<rolelist.size()-1;
#endif
}
#ifdef test
for(auto l:rolemap){
cout<<"inrolemap";
cout<<l.first<<' '<<l.second<<endl;
}
#endif
cin>>u;
for(int i=0;i<u;i++){
cin>>s;
cin>>temp;
vector<string> q;
for(int jj=0;jj<temp;jj++){
cin>>ss;
q.push_back(ss);
}
user newuser=user(s,q);
userlist.push_back(newuser);
usermap[s]=i;
}
cin>>q;
vector<int>ans;
for(int i=0;i<q;i++){
cin>>s;
cin>>ss;
if(usermap.count(s)==0){
#ifdef test
cout<<"not found "<<s<<endl;
#endif
ans.push_back(-2);
continue;
}
user thisuser=userlist[usermap[s]];
int iop=ss.find(':');
if(iop!=ss.npos){
int level=atoi(ss.substr(iop+1).c_str());
ss=ss.substr(0,iop);
if(thisuser.myuserp.count(ss)==0){
#ifdef test
cout<<"no priv "<<ss<<endl;
#endif
ans.push_back(-2);
continue;
}else{
if(thisuser.myuserp[ss]<level){
#ifdef test
cout<<"priv too low "<<ss<<" "<<level<<endl;
#endif
ans.push_back(-2);
continue;
}
#ifdef test
cout<<"priv too correct "<<ss<<endl;
#endif
ans.push_back(-1);
continue;
}
}else{
if(thisuser.myuserp.count(ss)==0){
#ifdef test
cout<<"no priv "<<ss<<endl;
#endif
ans.push_back(-2);
continue;
}else{
if(thisuser.myuserp[ss]==-1) {
#ifdef test
cout<<"yes priv "<<ss<<endl;
#endif
ans.push_back(-1);
continue;
}
ans.push_back(thisuser.myuserp[ss]);
continue;
}
}
}
for(int i:ans){
if(i==-2){
cout<<"false\n";
}else if(i==-1){
cout<<"true\n";
}else{
cout<<i<<endl;
}
}
}

浙公网安备 33010602011771号