这道题一开始我想用map表存储数据,后来才意识到这是一种错误的想法。过多的map会大大增加编程的复杂度。参考题目中给出的最多只有一百个权限、一百个角色和一百个用户名,所以这道题最理想的做法是用遍历穷举的方式一个一个去找。
100分代码如下,存在一定的冗余但是并不影响最后的效果。
#include <iostream>
#include <map>
#include <vector>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<set>
#include<bitset>
#include<stack>
#include<cstring>
#include<string>
using namespace std;
#define MAX 110
struct QX
{
string name;
int rank;
}qx[MAX];
struct Role
{
string name;
QX qxs[11];
int cntquan;
}roles[MAX];
struct Name
{
string name;
Role roles[11];
int cntrolt;
}user[MAX];
int c1, c2, c3,c4;
Role findrole(string role)
{
for (int i = 1; i <= c2; i++)
if (roles[i].name == role)
return roles[i];
}
bool find_name(string name)
{
for (int i = 1; i <= c3; i++)
if (user[i].name == name)
return true;
return false;
}
//在权限表中寻找一个权限
bool find_qx(string qxname,int rank)
{
for (int i = 1; i <= c1; i++)
{
if (qx[i].name == qxname&&qx[i].rank>=rank)
{
return true;
}
}
return 0;
}
void find_name_qx(string username, string inpu)
{
if (!find_name(username))
{
cout << "false" << endl;
return;
}
int loc = inpu.find(':');
string qxname;// = inpu.substr(0, loc);
int rank = 10;
if (loc != string::npos)
{
qxname = inpu.substr(0, loc);
rank = atoi(inpu.substr(loc + 1).c_str());
}
else
{
qxname = inpu;
}
int finalans = -1;
//cout << username << " " << inpu << endl;
//cout << qxname << " " << rank << endl;
for (int i = 1; i <= c3; i++)
{
if (user[i].name == username)
{
for (int j = 1; j <= user[i].cntrolt; j++)
{
Role role = user[i].roles[j];
for (int k = 1; k <=role.cntquan; k++)
{
if (role.qxs[k].name == qxname)
{
// cout<<"------" << role.qxs[k].name << endl;
if (rank == 10&&role.qxs[k].rank==10)
{
cout << "true" << endl;
return;
}
else if(role.qxs[k].rank!=10)
{
finalans = max(finalans, role.qxs[k].rank);
}
}
}
}
}
}
if (finalans == -1)
{
// cout << "fina" << endl;
cout << "false" << endl;
return ;
}
else
{
if (find_qx(qxname, finalans))
{
if (rank == 10)
{
cout << finalans << endl;
}
else if (rank <= finalans)
{
cout << "true" << endl;
}
else {
cout << "false" << endl;
}
}
else
{
cout << "false" << endl;
}
}
}
QX find(string qx1)
{
for(int i=1;i<=c1;i++)
if (qx[i].name == qx1)
{
return qx[i];
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("sb.txt", "r", stdin);
#endif // !ONLINE_JUDGE
ios::sync_with_stdio(false);
cin.tie(0);
cin >> c1;
for (int i = 1; i <= c1; i++)
{
string t;
cin >>t;
int loc = t.find(':');
if (loc != string::npos)
{
qx[i].name = t.substr(0, loc);
qx[i].rank = atoi(t.substr(loc + 1).c_str());
}
else
{
qx[i].name = t;
qx[i].rank = 10;
}
}
cin >> c2;
for (int i = 1; i <= c2; i++)
{
string rolename;
int cnt;
cin >> rolename >> cnt;
roles[i].name = rolename;
for (int j = 1; j <= cnt; j++)
{
string str;
cin >> str;
string qxname;
int loc = str.find(':');
int rank = 10;
if (loc == string::npos)
{
qxname = str;
}
else {
qxname = str.substr(0, loc);
rank = atoi(str.substr(loc + 1).c_str());
}
roles[i].cntquan++;
roles[i].qxs[roles[i].cntquan].name = qxname;
roles[i].qxs[roles[i].cntquan].rank = rank;
}
}
cin >> c3;
for (int i = 1; i <= c3; i++)
{
string username;
int cnt;
cin >> username >> cnt;
user[i].name = username;
for (int j = 1; j <= cnt; j++)
{
string rolename;
cin >> rolename;
user[i].cntrolt++;
user[i].roles[user[i].cntrolt]= findrole(rolename);
}
}
cin >> c4;
for (int i = 1; i <= c4; i++)
{
string name, item;
cin >> name >> item;
find_name_qx(name, item);
}
return 0;
}