CSP 202206-3 角色授权

一个简单的stl模拟,需要注意的点是读清题意,区分用户和角色的概念,还有一个坑点是用户的用户组每一次都是不固定的,需要每次输入重新绑定

#include <bits/stdc++.h>
using namespace std;
struct User {
    set<string> group;
};
struct Role {
    set<string> op;
    set<string> rKinds;
    set<string> rNames;
};
map<string,User> nameToUser;//根据用户名找到用户
map<string,Role> nameToRole;//根据角色名找到角色
map<string,set<string>> userContainRole;//用户与角色关联
map<string,set<string>> groupContainRole;//用户组与角色关联
void readRole() {
    Role role;
    string roleName;
    cin >> roleName;
    int n; cin >> n;
    for(int i = 0; i < n; i++) {
        string op;
        cin >> op;
        role.op.insert(op);
    }
    cin >> n;
    for(int i = 0; i < n; i++) {
        string rKinds;
        cin >> rKinds;
        role.rKinds.insert(rKinds);
    }
    cin >> n;
    for(int i = 0; i < n; i++) {
        string rNames;
        cin >> rNames;
        role.rNames.insert(rNames);
    }
    nameToRole[roleName] = role;
}
void connect() {
    string roleName; cin >> roleName;
    int n; cin >> n;
    for(int i = 0; i < n; i++) {
        string prop, name;
        cin >> prop >> name;
        if(prop == "u") {
            userContainRole[name].insert(roleName);
        } else {
            groupContainRole[name].insert(roleName);
        }
    }
}
bool checkRoleCanDo(Role& role,string& op, string& rKind, string& rName) {
    bool canDo = true;
    if(!role.op.count(op) && !role.op.count("*")) {
        canDo = false;
    }
    if(!role.rKinds.count(rKind) && !role.rKinds.count("*")) {
        canDo = false;
    }
    if(!role.rNames.count(rName) && !role.rNames.empty()) {
        canDo = false;
    }
    return canDo;
}
bool checkUserRole(string& userName, string& op, string& rKind, string& rName) {
    auto& roleNames = userContainRole[userName];
    for(auto& roleName : roleNames) {
        auto& role = nameToRole[roleName];
        bool canDo = checkRoleCanDo(role,op,rKind,rName);
        if(canDo) {return true;}
    }
    return false;
}
bool checkGroupRole(string& userName, string& op, string& rKind, string& rName) {
    auto& groups = nameToUser[userName].group;
    for(auto& gName : groups) {
        auto& roleNames = groupContainRole[gName];
        for(auto& roleName : roleNames) {
            auto& role = nameToRole[roleName];
            if(checkRoleCanDo(role,op,rKind,rName)) {return true;}
        }
    }
    return false;
}
void check() {
    string userName; int ng;
    string op,rKind,rName;
    cin >> userName;
    cin >> ng;
    nameToUser[userName].group.clear();
    for(int i = 0; i < ng; i++) {
        string gName; cin >> gName;
        nameToUser[userName].group.insert(gName);
    }
    bool res = false;
    cin >> op >> rKind >> rName;
    res = checkUserRole(userName,op,rKind,rName);
    if(res) {
        cout << 1 << '\n'; return;
    }
    res = checkGroupRole(userName, op, rKind, rName);
    if(res) {
        cout << 1 << '\n'; return;
    } else {
        cout << 0 << '\n'; return;
    }
}
int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n,m,q;
    cin >> n >> m >> q;
    for(int i = 0; i < n; i++) {
        readRole();
    }
    for(int i = 0; i < m; i++) {
        connect();
    }
    for(int i = 0; i < q; i++) {
        check();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值