csp201612-3 权限查询

思路解析

有点难受,这道题在CSP官网过了,但是在acwing上面没过。

2744728	魏子尧	魏子尧	权限查询	10-06 21:16	1.835KB	CPP14	正确	100	109ms	3.152MB

写这道题的时候想着肯定不能遍历查询(遍历权限查询其实也能过)

所以map的结构是:

map<人名, 结构体{set<所有权限名>; map<带等级的权限名, 权限等级>}>

这样在查找的时候,可以先查询set中的所有权限名, 再去进行是否带有等级的分类讨论。

代码

#include<iostream>
#include<vector>
#include<map>
#include<set>
using namespace std;


map<string, vector<string>> rl_mp;

class person {
public:
	set<string> st;
	map<string, int> grds;
	string name;
};

map<string, person> ps_mp;

int main() {
	int p; cin >> p;
	for (int i = 0; i < p; i++) {
		string cate; cin >> cate;
	}

	int r; cin >> r;
	for (int i = 0; i < r; i++) {
		string rl; cin >> rl;
		int j; cin >> j;
		vector<string> vec;
		while (j--) {
			string cate; cin >> cate;
			vec.push_back(cate);
		}

		rl_mp.emplace(rl, vec);
	}

	int u; cin >> u;
	for (int i = 0; i < u; i++) {
		string name; cin >> name;
		int cnt; cin >> cnt;
		person p1;
		while (cnt--) {
			string str; cin >> str;
			vector<string> n = rl_mp.at(str);
			for (auto& v : n) {
				if (v[v.size() - 2] == ':') {
					int g = v[v.size() - 1] - '0';
					string sub = v.substr(0, v.size() - 2);
					if (p1.grds.find(sub) != p1.grds.end()) {
						if (p1.grds.at(sub) < g) {
							p1.grds[sub] = g;
						}
					}
					else p1.grds.emplace(sub, g);
					p1.st.insert(sub);
				}
				else p1.st.insert(v);
			}
		}

		ps_mp.emplace(name, p1);
	}

	int q; cin >> q;
	while (q--) {
		string name; cin >> name;
		if (ps_mp.find(name) == ps_mp.end()) {
			cout << "false" << endl;
			continue;
		}
		person p1 = ps_mp.at(name);
		string qry; cin >> qry;
		if (qry[qry.size() - 2] == ':') {
			int g = qry[qry.size() - 1] - '0';
			string sub = qry.substr(0, qry.size() - 2);
			if (p1.grds.find(sub) != p1.grds.end() && p1.grds[sub] >= g) {
				cout << "true";
			}
			else cout << "false"; 
			cout << endl;
			continue;
		}
		else {
			if (p1.st.find(qry) != p1.st.end() && p1.grds.find(qry) == p1.grds.end()) {
				cout << "true" << endl;
				continue;
			}
			else {
				if (p1.st.find(qry) == p1.st.end()) cout << "false";
				else cout << p1.grds[qry];
				cout << endl;
			}
		}
	}
	return 0;
}