CCF-权限查询-201612-3

时间:2023-03-10 05:58:00
CCF-权限查询-201612-3

这道题,开始只有10分.....原因是将false 写成了 flase

我要吐血而亡....关键是还debug了半天,以为是逻辑错了

不过亮点是代码很简洁,网上140+的代码看着真复杂

核心:

做题之前首先要理好思路,读清楚题意,不要担心在写代码上浪费时间.思路清晰写起来很快的

首先设计好数据结构,然后设计一个权限类型的输入函数,将三种判别方法发合并,总结出简洁的方式判断

首先去无权限的默认权限是0;

查询的时候查询的是最高权限,初始-1

如果查到了权限小于要求的权限(要求的权限没有也默认为0) 就是no

否则再判断是否是有等级的无等级查询 是从\输出等级还是输出yes

 #include <bits/stdc++.h>
using namespace std;
const int N=;
map <string,int> map1,map2,map3;
struct node {
string na;
bool flag;
int lev;
};
node p[N];
vector <node> role[N];
vector <int> user[N];
int np,nr,nu,q,ans;
node input_p() {
string str; cin>>str;
node tmp={"",,}; int i=;
while (i<str.size()&&str[i]!=':') tmp.na+=str[i++];
if (str[i]==':') {
tmp.lev=str[i+]-'';
tmp.flag=;
}
return tmp;
}
void find_p (int x,string na) {
for (int i=;i<user[x].size();i++) {
int k=user[x][i];
for (int j=;j<role[k].size();j++) {
node nxt=role[k][j];
if (na==nxt.na) ans=max(ans,nxt.lev);
}
}
}
int main ()
{
cin>>np;
for (int i=;i<=np;i++) {
p[i]=input_p();
map1[p[i].na]=i;
}
cin>>nr;
for (int i=;i<=nr;i++) { /// role - > p
string str; cin>>str; map2[str]=i;
int num; cin>>num;
for (int j=;j<=num;j++) {
node tmp=input_p();
role[i].push_back(tmp);
}
}
cin>>nu;
for (int i=;i<=nu;i++) {
string str; cin>>str; map3[str]=i;
int num; cin>>num;
for (int j=;j<=num;j++) {
string tmp; cin>>tmp;
user[i].push_back(map2[tmp]);
}
}
cin>>q;
while (q--) {
string str; cin>>str; int x=map3[str];
node tmp=input_p();
ans=-; string na=tmp.na; int id=map1[na];
find_p(x,na);
if (!tmp.flag&&p[id].flag) {
if (ans<) cout<<"false\n";
else cout<<ans<<"\n";
}
else {
if (ans<tmp.lev) cout<<"false\n";
else cout<<"true\n";
}
}
return ;
}