题目太长就只放连接了:计算机软件能力认证考试系统
思路分析:此题是大模拟,我用到了pair,set和map三种数据结构。要熟练运用其中的empty(),count(),insert(),clear()等函数。
读懂题后,发现不用在用户组中维护用户列表,只需要在每个用户中添加用户组即可。而且添加角色关联的时候可以向用户或用户组数据结构的role集合中添加。最后判断某个用户,只需要遍历用户中的role集合和其所属的用户组集合中不同用户组的role集合即可。总之就是理清题意,最优化解决方案。
得分情况:最初没有考虑到“每次用户查询权限要清空之前的用户所属组”细节,得分30
修改后超时,得分80
最后加上一句话 ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);优化输入时间,得分100
代码:
#include<bits/stdc++.h>
using namespace std;
struct group
{
public:
set<string> group_role;
};
struct role
{
public:
set<string> options;
set<string> resource;
set<string> r_name;
};
map<string,role> map_role;
map<string,group> map_group;
struct user
{
public:
set<string> user_group;
set<string> user_role;
bool check_empower(string o,string r,string n){
set<string>::iterator it = user_role.begin();
for(;it!=user_role.end();it++){
if(map_role[*it].options.count("*")||map_role[*it].options.count(o))
if(map_role[*it].resource.count("*")||map_role[*it].resource.count(r))
if(map_role[*it].r_name.count(n)||map_role[*it].r_name.empty())
return true;
}
set<string>::iterator it2 = user_group.begin();
for(;it2!=user_group.end();it2++){
it =map_group[*it2].group_role.begin();
for(;it!=map_group[*it2].group_role.end();it++){
if(map_role[*it].options.count("*")||map_role[*it].options.count(o))
if(map_role[*it].resource.count("*")||map_role[*it].resource.count(r))
if(map_role[*it].r_name.count(n)||map_role[*it].r_name.empty())
return true;
}
}
return false;
}
};
map<string,user> map_user;
int main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);//80-100
int n,m,q;
cin>>n>>m>>q;
while (n--)
{
string rolename;
int nv,no,nn;
cin>>rolename;
role temprole;
string str;
cin>>nv;
for(int i=0;i<nv;i++){
cin>>str;
temprole.options.insert(str);
}
cin>>no;
for(int i=0;i<no;i++){
cin>>str;
temprole.resource.insert(str);
}
cin>>nn;
for(int i=0;i<nn;i++){
cin>>str;
temprole.r_name.insert(str);
}
map_role.insert(pair<string,role>(rolename,temprole));
}
while(m--){
string role;
string str;
string ugname;
int ns;
cin>>role>>ns;
for(int i=0;i<2*ns;i+=2){
cin>>str;
cin>>ugname;
if(str=="u"){
if(map_user.find(ugname)!=map_user.end()){
map_user[ugname].user_role.insert(role);
}
else{
user tempuser;
tempuser.user_role.insert(role);
map_user.insert(pair<string,user>(ugname,tempuser));
}
}
else{
if(map_group.find(ugname)!=map_group.end()){
map_group[ugname].group_role.insert(role);
}
else{
group tempgroup;
tempgroup.group_role.insert(role);
map_group.insert(pair<string,group>(ugname,tempgroup));
}
}
}
}
while(q--){
string uname;
int ng;
string o,r,n;
cin>>uname;
cin>>ng;
if(map_user.find(uname)==map_user.end()){
user u;
map_user.insert(pair<string,user>(uname,u));
}
map_user[uname].user_group.clear();//30-80
while(ng--){
string group;
cin>>group;
map_user[uname].user_group.insert(group);
}
cin>>o>>r>>n;
if(map_user[uname].check_empower(o,r,n)) cout<<1<<endl;
else cout<<0<<endl;
}
}