使用C++的CCF-CSP满分解决方案 202206-3 角色授权

题目过长,就不贴了……

解题思路

反复阅读题目,会发现其实用户和用户组是一回事,二者是并列的关系,而不是包含的关系,所以没必要把二者区分。
每个角色用名称标识,再加上三个集合存储信息,然后就没什么难的了。

注意事项

在写的时候遇到了如下问题:

  • 运行时间过长
    在这里插入图片描述

解决方法是加上一句话就好:

//提高cin,cout的速度 
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

  • 注意理解题意
    我最开始以为是所有的权限共享,后来写错之后发现,原来是只能用一个角色来过三个关卡的。

满分代码实现:

#include 
#include 
#include 
#include 
#include 
using namespace std;

/*
class Client {
public:
	string name;
	string kind;
	
	Client(const string& name, const string& kind) : name(name), kind(kind) {}
}; 
*/

class Role {
public:
	string name;
	set<string> operation;
	set<string> sourceKind;
	set<string> sourceName;
	
	Role(string name, set<string> operation, set<string> sourceKind,
	set<string> sourceName) : name(name), operation(operation), sourceKind(sourceKind),
	sourceName(sourceName) {}
	
    bool operator<(const Role &other) const {
        return name <= other.name;
	}	
};

/*
class Connect {
public:
	map _map;
};
*/

class Solve {
private:
	unordered_map<string, Role> roles;
	unordered_map<string, vector<string> > connect;
	void getRole(int n);
	void getConnect(int m);
	void judgePrint();
	bool judge(const vector<string>& names);
public:
	void solve();
};

inline void Solve::getRole(int n) {
	string name;
	set<string> operation;
	set<string> sourceKind;
	set<string> sourceName;
	string tmp;
	int a = 0;
	for (int i = 0; i < n; i++) {
		operation.clear();
		sourceKind.clear();
		sourceName.clear();
		cin >> name;
		cin >> a;
		for (int j = 0; j < a; j++) {
			cin >> tmp;
			operation.insert(tmp);
		}
		cin >> a;
		for (int j = 0; j < a; j++) {
			cin >> tmp;
			sourceKind.insert(tmp);
		}
		cin >> a;
		for (int j = 0; j < a; j++) {
			cin >> tmp;
			sourceName.insert(tmp);
		}
		roles.insert(make_pair(name, Role(name, operation, sourceKind, sourceName)));
	}
}

inline void Solve::getConnect(int m) {
	int num = 0;
	string roleName;
	string clientName;
	for (int i = 0; i < m; i++) {
		cin >> roleName;
		cin >> num;
		for (int j = 0; j < num; j++) {
			cin >> clientName;
			cin >> clientName;
			connect[clientName].push_back(roleName);
		}
	}
}

inline void Solve::solve() {
	int n = 0, m = 0, q = 0;
	cin >> n >> m >> q;
	getRole(n); 
	getConnect(m);
	for (int i = 0; i < q; i++) {
		judgePrint();
	}	
}

inline void Solve::judgePrint() {
	string name;
	vector<string> names;
	cin >> name;
	names.push_back(name);
	int num = 0;
	cin >> num;
	for (int i = 0; i < num; i++) {
		cin >> name;
		names.push_back(name);
	}
	if (judge(names)) {
		cout << 1 << endl;
	}
	else {
		cout << 0 << endl;
	}
}
inline bool Solve::judge(const vector<string>& names) {
	string name, oper, kind, sname;
	cin >> oper >> kind >> sname;
	for (string name : names) {
		if (!connect.count(name)) continue;
		const vector<string>& vec = connect[name];
		for (string roleName : vec) {
			const Role& r = roles.at(roleName);
			if (r.operation.count("*") || r.operation.count(oper)) {
				if (r.sourceKind.count("*") || r.sourceKind.count(kind)) {
					if (r.sourceName.empty() || r.sourceName.count(sname)) {
						return true;
					}
				}
			}
		}
	}
	return false;
}

int main() {
	//提高cin,cout的速度 
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	Solve solution;
	solution.solve();
	return 0;
}

你可能感兴趣的:(CCF-CSP学习之路,c++,算法,开发语言)