权限查询

//100

#include<iostream>
#include<sstream>
#include<algorithm>
#include<string.h>
#include<iomanip>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<map>

using namespace std;

typedef pair<string, int> PII;

//vector<PII> P, R;
const int N = 1000+5;
map<string, vector<string> > U;
vector<string> P, R[N];
int p, r, u, q;

int main() {
	ios::sync_with_stdio(false);
	cin >> p;
	string s, s1;
	cin.get();

	for(int i=0; i<p; i++) {
		getline(cin, s);
		P.push_back(s);
	}

	cin >> r;
	cin.get();
	for(int i=0; i<r; i++) {
		getline(cin, s);
		stringstream ss(s);
		ss >> s;
		R[i].push_back(s);
		ss >> s;

		while(ss >> s) {
			R[i].push_back(s);
		}
	}
	/*
	cout << "不同的角色及其权限:" << endl;
	for(int i=0; i<r; i++) {
		for(int j=0; j<R[i].size(); j++) {
			cout << R[i][j] << " ";
		}
		cout << endl;
	}*/




	cin >> u;
	cin.get();
	for(int i=0; i<u; i++) {
		getline(cin, s);
		stringstream ss(s);
		ss >> s;
		vector<string> tmp;
		ss >> s1;
		while(ss >> s1) {
			tmp.push_back(s1);
		}
		U[s] = tmp;
	}
	/*
		cout << "用户及其拥有的角色:" << endl;
		map<string, vector<string> >::iterator tm=U.begin();

		for(; tm!=U.end(); tm++) {
			cout << tm -> first <<":";

			vector<string>tmp = tm->second;

			for(int i=0; i<tmp.size(); i++) {
				cout << tmp[i] << " ";
			}
			cout << endl;
		}
	*/
	cin >> q;
	cin.get();
	for(int i=0; i<q; i++) {
		getline(cin, s);
		stringstream ss(s);
		bool flag = false;
		ss >> s;

		//判断是否有该用户
		map<string, vector<string> >::iterator it=U.begin();

		for(; it!=U.end(); it++) {
			if(it -> first == s) {
				flag = true;
				break;
			}
		}
		if(!flag) { //不存在这个人
			cout << "false" << endl;
			continue;
		}

		ss >> s;
		int pos = s.find(':'), l;
		if(pos != string::npos) {
			s1 = s.substr(0, pos);
			string s2 = s.substr(pos+1);
			l = s2[0]-'0';//0-9
		} else {
			s1 = s, l = -1;
		}

		vector<string>tmp = it->second;

//		cout << "查询的用户为:" << it->first << endl<<"查询的权限为:"<<s1 << l<< endl <<"用户所拥有的角色有:"<< endl;
		flag = false;
		int rn=-1;
		for(int i=0; i<tmp.size(); i++) {
//			cout << tmp[i] << endl;
//			cout << "该角色的权限为:"<<endl;
			
			for(int j=0; j<r; j++) {
				if(R[j][0] == tmp[i]) {
					//查看该角色下的权限
					for(int k=1; k<R[j].size(); k++) {
						string st = R[j][k], st1;
//						cout << st << "划分为:";
						if(st == s) {
//							cout<<st << "直接符合:" << s << endl;
							flag = true;
							break;
						}
						int post = st.find(':'), lt;
						if(post != string::npos) {
							st1 = st.substr(0, post);
							string st2 = st.substr(post+1);
							lt = st2[0]-'0';//0-9
						} else {
							st1 = st, lt = -1;
						}
//						cout << st1 << lt << endl;
						if(st1 == s1 && l==-1) {
							flag = true;
							//	cout << "已找到符合权限,但需寻求最大值" << endl;
							rn = max(rn, lt);
						}else if(st1 == s1 && l <= lt) {
//							cout << "已找到符合的权限" << endl;
//							cout << "true" << endl;
							flag = true;
							break;
						} 
					}
					break;//已找到该角色,不需要再找其他角色了 
				}

			}
//			cout << flag << "-------------------------------------" << endl;
		}
		if(!flag) {
			cout << "false" << endl;
		} else if(rn == -1) cout << "true" << endl;
		else cout << rn << endl;
	}
	return 0;
}

 

//40分    flag标记位置放错了.....修改下就100.....

 

#include<iostream>
#include<sstream>
#include<algorithm>
#include<string.h>
#include<iomanip>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<map>

using namespace std;

typedef pair<string, int> PII;

//vector<PII> P, R;
const int N = 100+5;
map<string, vector<string> > U;
vector<string> P, R[N];
int p, r, u, q;

int main() {
	cin >> p;
	string s, s1;
	cin.get();

	for(int i=0; i<p; i++) {
		getline(cin, s);
		P.push_back(s);
	}

	cin >> r;
	cin.get();
	for(int i=0; i<r; i++) {
		getline(cin, s);
		stringstream ss(s);
		ss >> s;
		R[i].push_back(s);
		ss >> s;

		while(ss >> s) {
			R[i].push_back(s);
		}
	}
	/*
	cout << "不同的角色及其权限:" << endl;
	for(int i=0; i<r; i++) {
		for(int j=0; j<R[i].size(); j++) {
			cout << R[i][j] << " ";
		}
		cout << endl;
	}*/




	cin >> u;
	cin.get();
	for(int i=0; i<u; i++) {
		getline(cin, s);
		stringstream ss(s);
		ss >> s;
		vector<string> tmp;
		ss >> s1;
		while(ss >> s1) {
			tmp.push_back(s1);
		}
		U[s] = tmp;
	}
	/*
		cout << "用户及其拥有的角色:" << endl;
		map<string, vector<string> >::iterator tm=U.begin();

		for(; tm!=U.end(); tm++) {
			cout << tm -> first <<":";

			vector<string>tmp = tm->second;

			for(int i=0; i<tmp.size(); i++) {
				cout << tmp[i] << " ";
			}
			cout << endl;
		}
	*/
	cin >> q;
	cin.get();
	for(int i=0; i<q; i++) {
		getline(cin, s);
		stringstream ss(s);
		bool flag = false;
		ss >> s;

		//判断是否有该用户
		map<string, vector<string> >::iterator it=U.begin();

		for(; it!=U.end(); it++) {
			if(it -> first == s) {
				flag = true;
				break;
			}
		}
		if(!flag) { //不存在这个人
			cout << "false" << endl;
			continue;
		}

		ss >> s;
		int pos = s.find(':'), l;
		if(pos != string::npos) {
			s1 = s.substr(0, pos);
			string s2 = s.substr(pos+1);
			l = s2[0]-'0';//0-9
		} else {
			s1 = s, l = -1;
		}

		vector<string>tmp = it->second;

//		cout << "查询的用户为:" << it->first << endl<<"查询的权限为:"<<s1 << l<< endl <<"用户所拥有的角色有:"<< endl;
		int rn=-1;
		for(int i=0; i<tmp.size(); i++) {
//			cout << tmp[i] << endl;
//			cout << "该角色的权限为:"<<endl;
			flag = false;
			for(int j=0; j<r; j++) {
				if(R[j][0] == tmp[i]) {
					//查看该角色下的权限
					for(int k=1; k<R[j].size(); k++) {
						string st = R[j][k], st1;
//						cout << st << "划分为:";
						if(st == s) {
//							cout<<st << "直接符合:" << s << endl;
							flag = true;
							break;
						}
						int post = st.find(':'), lt;
						if(post != string::npos) {
							st1 = st.substr(0, post);
							string st2 = st.substr(post+1);
							lt = st2[0]-'0';//0-9
						} else {
							st1 = st, lt = -1;
						}
//						cout << st1 << lt << endl;
						if(st1 == s1 && l <= lt && l>=0) {
//							cout << "已找到符合的权限" << endl;
//							cout << "true" << endl;
							flag = true;
							break;
						} else if(st1 == s1 && l==-1) {
							flag = true;
	//						cout << "已找到符合权限,但需寻求最大值" << endl;
							rn = max(rn, lt);
						}
					}
					break;
				}
				
			}
//			cout << flag << "-------------------------------------" << endl; 
		
		}	if(!flag) {
				cout << "false" << endl;
			}else if(rn == -1) cout << "true" << endl;
			else cout << rn << endl;
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值