一共有三层信息,三层信息的依赖关系是:
\[用户user->角色role->权限authority
\]
\]
先存储\(authority\)信息,\(role\)直接存储\(authority\)对应序号的信息,\(user\)通过\(role\)直接存储的是\(authority\)对应序号的信息。
剩下的就是字符串处理了。
唯一的坑点大概就是查询的时候\(user\)和\(authority\)名字可能没有出现过。
#include <bits/stdc++.h>
const int maxn = 100;
using namespace std;
struct tAuthority
{
char category[40];
bool hasLevel;
int level;
};
tAuthority authority[maxn + 5];
struct tRole
{
char role[40];
bool hasAuthority[maxn + 5];
int level[maxn + 5];
tRole()
{
memset(hasAuthority, 0, sizeof(hasAuthority));
memset(level, -1, sizeof(level));
}
};
tRole role[maxn + 5];
struct tUser
{
char user[40];
bool hasAuthority[maxn + 5];
int level[maxn + 5];
tUser()
{
memset(hasAuthority, 0, sizeof(hasAuthority));
memset(level, -1, sizeof(level));
}
};
tUser user[maxn + 5];
int main()
{
int p;
scanf("%d", &p);
for (int i = 1; i <= p; i++)
{
char temp[40];
scanf("%s", temp);
int len = strlen(temp);
if (temp[len - 2] == ':')
{
authority[i].hasLevel = true;
authority[i].level = temp[len - 1] - '0';
temp[len - 2] = '\0';
}
else
{
authority[i].hasLevel = false;
}
strcpy(authority[i].category, temp);
}
int r;
scanf("%d", &r);
for (int i = 1; i <= r; i++)
{
scanf("%s", role[i].role);
int s;
scanf("%d", &s);
while (s--)
{
char temp[40];
scanf("%s", temp);
int len = strlen(temp);
if (temp[len - 2] == ':')
{
temp[len - 2] = '\0';
}
for (int j = 1; j <= p; j++)
{
if (strcmp(authority[j].category, temp) == 0)
{
role[i].hasAuthority[j] = true;
if (temp[len - 1] >= '0' && temp[len - 1] <= '9')
role[i].level[j] = max(role[i].level[j], temp[len - 1] - '0');
break;
}
}
}
}
int u;
scanf("%d", &u);
for (int i = 1; i <= u; i++)
{
scanf("%s", user[i].user);
int t;
scanf("%d", &t);
while (t--)
{
char temp[40];
scanf("%s", temp);
for (int j = 1; j <= r; j++)
{
if (strcmp(role[j].role, temp) == 0)
{
for (int k = 1; k <= p; k++)
{
if (role[j].hasAuthority[k])
{
user[i].hasAuthority[k] = true;
user[i].level[k] = max(user[i].level[k], role[j].level[k]);
}
}
break;
}
}
}
}
int q;
scanf("%d", &q);
while (q--)
{
char temp1[40], temp2[40];
bool flag1 = false, flag2 = false;
scanf("%s%s", temp1, temp2);
int len = strlen(temp2);
if (temp2[len - 2] == ':')
temp2[len - 2] = '\0';
for (int i = 1; i <= u; i++)
{
if (strcmp(user[i].user, temp1) == 0)
{
flag1 = true;
int id = 0;
for (; id <= p && strcmp(authority[id].category, temp2) != 0; id++);
if (id <= p)
{
flag2 = true;
// printf("query case: %d %d %s\n", i, id, authority[id].category);
if (user[i].hasAuthority[id])
{
if (temp2[len - 1] >= '0' && temp2[len - 1] <= '9')
{
if (user[i].level[id] >= temp2[len - 1] - '0')
printf("true\n");
else
printf("false\n");
}
else
{
if (authority[id].hasLevel)
printf("%d\n", user[i].level[id]);
else
printf("true\n");
}
}
else
{
printf("false\n");
}
}
break;
}
}
if (!flag1 || !flag2)
printf("false\n");
}
return 0;
}