一道思路很清楚的模拟题。
模拟题最重要的就是理清出题人的思路,在这基础上找到合适的数据结构,加以简单的算法即可实现。
本题大致分为三层关系:
权限-等级,权限-角色,角色-用户。
显然,这是一对一或一对多的关系,考虑使用map来存储这些关系。
其中,qx对应权限层,即<权限名,权限等级>;
js对应角色层,即角色名——vector<pair<权限名,权限等级>>;
yh对应用户层,即用户名——vector<角色名>
话不多说上代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn = 105;
const int INF = 99999999;
map<string,int> qx;
map<string, vector<pair<string,int> > > js;
map<string, vector<string> > yh;
int p, r, u, Q;
string s;
int str2int(string s) {
int num = 0;
for (int i = 0;i < s.size();i++)num += (s[i] - '0') * 10;
return num;
}
int main()
{
ios::sync_with_stdio(false);
cin >> p;
for (int i = 0;i < p;i++) {
cin >> s;
if (s.find(':') == string::npos) qx[s.substr(0, s.find(':'))] = -1;
else qx[s.substr(0, s.find(':'))] = stoi(s.substr(s.find(':') + 1));
}
cin >> r;
int k;
string name, q;
for (int i = 0;i < r;i++) {
cin >> name >> k;
for (int j = 0;j < k;j++) {
cin >> q;
int n = -1;
if (q.find(':') != string::npos) {
n = stoi(q.substr(q.find(':') + 1));
q = q.substr(0, q.find(':'));
}
else n = qx[q];
js[name].push_back(make_pair(q, n));
}
}
cin >> u;
for (int i = 0;i < u;i++) {
cin >> name >> k;
for (int j = 0;j < k;j++) {
cin >> q;
yh[name].push_back(q);
}
}
cin >> Q;
while (Q--) {
cin >> name >> q;
if (yh.find(name) == yh.end()) {
cout << "false" << endl;
continue;
}
if (q.find(':') == string::npos) {
if (qx.find(q) == qx.end()) {
cout << "false" << endl;
continue;
}
int maxqx = -1, flag = 0;
for (auto tjs : yh[name]) {
int bk = 0;
for (auto tqx : js[tjs]) {
string na = tqx.first;
int nu = tqx.second;
if (na == q) {
maxqx = max(maxqx, nu);
flag = 1;
}
}
}
if (!flag)cout << "false" << endl;
else {
if (maxqx == -1)cout << "true" << endl;
else cout << maxqx << endl;
}
}
else {
if (qx.find(q.substr(0, q.find(':'))) == qx.end()) {
cout << "false" << endl;
continue;
}
int n = stoi(q.substr(q.find(':') + 1));
q = q.substr(0, q.find(':'));
int maxqx = -1;
for (auto tjs : yh[name]) {
for (auto tqx : js[tjs]) {
string na = tqx.first;
int nu = tqx.second;
if (na == q) maxqx = max(maxqx, nu);
}
}
if (maxqx < n)cout << "false" << endl;
else cout << "true" << endl;
}
}
return 0;
}