CSP-2022-06-3(角色授权)

  • 这道题思路上来讲不太难,主要就是去模拟题目中所说的工作,需要注意的是查询的时候复杂度尽量压缩才能保证时间不会超时,所以本人代码开了哈希表和O2优化进行时间的压缩

  • 当时做的时候一直超时没有想明白为什么,后来发现是在对set中的元素进行遍历的时候对这些元素又进行了进一步的合理性检查,但是其实这个检查我在将用户insertset的时候已经进行过了,所以这时候进行检查会增加许多时间复杂度,导致TLE

  • 改进代码也可以加入一些使用bitset压位的优化操作来减少空间复杂度

#include <bits/stdc++.h>
#define endl '\n'
#pragma GCC optimize(2)
typedef long long ll;
using namespace std;
const int N = 5e2 + 5;
int n, m, q;
struct actor {
    unordered_map<string, bool> op;
    unordered_map<string, bool> s_kind;
    unordered_map<string, bool> s_name;
    unordered_map<string, bool> group;
    unordered_map<string, bool> user;
};
unordered_map<string, int> actors;
vector<actor> act;
unordered_map<string, vector<string>> toName;
void solve() {
    cin >> n >> m >> q;
    act.resize(n);
    for (int i = 0; i < n; ++i) {
        int nv, no, nn;
        actor tmp;
        string name;
        cin >> name >> nv;
        for (int j = 0; j < nv; ++j) {
            string x; cin >> x;
            tmp.op[x] = true;
        }
        cin >> no;
        for (int j = 0; j < no; ++j) {
            string x; cin >> x;
            tmp.s_kind[x] = true;
        }
        cin >> nn;
        for (int j = 0; j < nn; ++j) {
            string x; cin >> x;
            tmp.s_name[x] = true;
        }
        act[i] = tmp;
        actors[name] = i;
    }
    for (int i = 0; i < m; ++i) {
        string name; cin >> name;
        int ns; cin >> ns;
        for (int j = 0; j < ns; ++j) {
            char f;
            string u; cin >> f >> u;
            toName[u].push_back(name);
            if (f == 'g') act[actors[name]].group[u] = true;
            if (f == 'u') act[actors[name]].user[u] = true;
        }
    }
    for (int i = 0; i < q; ++i) {
        bool res = false;
        string usr; cin >> usr;
        int ng; cin >> ng;
        vector<string> gp(N);
        set<string> tmpName;
        for (int j = 0; j < ng; ++j) {
            cin >> gp[j];
            if (toName.find(gp[j]) != toName.end()) {
                for (auto x : toName[gp[j]]) {
                    tmpName.insert(x);
                }
            }
        }
        if (toName.find(usr) != toName.end()) {
            for (auto x : toName[usr]) {
                tmpName.insert(x);
            }
        }
        string a, b, c; cin >> a >> b >> c;
        for (auto x : tmpName) {
            bool f1 = false, f2 = false, f3 = false;
            if ((act[actors[x]].op.find(a) != act[actors[x]].op.end()) || 
            (act[actors[x]].op.find("*") != act[actors[x]].op.end())) f1 = true;
            if ((act[actors[x]].s_kind.find(b) != act[actors[x]].s_kind.end()) || 
            (act[actors[x]].s_kind.find("*") != act[actors[x]].s_kind.end())) f2 = true;
            if ((act[actors[x]].s_name.empty()) || (act[actors[x]].s_name.find(c) 
            != act[actors[x]].s_name.end())) f3 = true;
            if (f1 && f2 && f3) {
                res = true;
                break;
            }
        }
        if (res) cout << 1 << endl;
        else cout << 0 << endl;
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    solve();
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值