題目鏈接,註冊了自己看把,那個認證題庫已經好幾天打不開了,沒辦法上鍊接了
中等模擬把,就是三層結構體嵌套。捋清楚就行。
AC代碼
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=110;
struct quanxian{
char ca[35];
int level=-1;
}qx[maxn];
struct juese{
char role[35];
int s;
quanxian jqx[15];
}js[maxn];
struct yonghu{
char user[35];
int t;
char yhj[15][35];
int to=0;
quanxian yqx[maxn];
}yh[maxn];
int main(){
int p,r,u,q;
int tqx=0;
scanf("%d",&p);
for(int i=0;i<p;i++){
char tmp[50];
scanf("%s",tmp);
// printf("%s\n",tmp);
int pos=-1;
for(int j=0;j<strlen(tmp);j++){
if(tmp[j]==':'){
pos=j;
break;
}
}
if(pos==-1) {
strcpy(qx[i].ca,tmp);
continue;
}
else {
strncpy(qx[i].ca,tmp,pos);
qx[i].level=tmp[pos+1]-'0';
}
}
/*check quanxian input*/
/*
for(int i=0;i<p;i++){
printf("%s %d\n",qx[i].ca,qx[i].level);
}
*/
scanf("%d",&r);
for(int i=0;i<r;i++){
char tmp[50];
int num;
scanf("%s%d",tmp,&num);
strcpy(js[i].role,tmp);
js[i].s=num;
for(int j=0;j<num;j++){
char tmp1[50];
scanf("%s",tmp1);
int pos=-1;
for(int k=0;k<strlen(tmp1);k++){
if(tmp1[k]==':'){
pos=k;
break;
}
}
if(pos==-1){
strcpy(js[i].jqx[j].ca,tmp1);
}else{
strncpy(js[i].jqx[j].ca,tmp1,pos);
js[i].jqx[j].level=tmp1[pos+1]-'0';
}
}
}
/*check role input*/
/*
for(int i=0;i<r;i++){
printf("%s\n",js[i].role);
for(int j=0;j<js[i].s;j++){
printf("%s %d\n",js[i].jqx[j].ca,js[i].jqx[j].level);
}
}
*/
scanf("%d",&u);
for(int i=0;i<u;i++){
char tmp[50];
int num;
scanf("%s %d",tmp,&num);
strcpy(yh[i].user,tmp);
yh[i].t=num;
for(int j=0;j<num;j++){
char tmp1[50];
scanf("%s",tmp1);
strcpy(yh[i].yhj[j],tmp1);
}
}
/*check yonghu input*/
/*
for(int i=0;i<u;i++){
printf("%s: ",yh[i].user);
for(int j=0;j<yh[i].t;j++){
printf("%s ",yh[i].yhj[j]);
}printf("\n");
}
*/
/*把用戶的所有權限全部取到用戶層次*/
for(int i=0;i<u;i++){
int tot=0;
for(int j=0;j<yh[i].t;j++){
char tmp[50];
strcpy(tmp,yh[i].yhj[j]);
// printf("%s ",tmp);
for(int k=0;k<r;k++){
if(strcmp(tmp,js[k].role)==0){
for(int l=0;l<js[k].s;l++){
bool cunzai=false;
for(int m=0;m<tot;m++){
if(strcmp(js[k].jqx[l].ca,yh[i].yqx[m].ca)==0){
yh[i].yqx[m].level=max(yh[i].yqx[m].level,js[k].jqx[l].level);
cunzai=true;
break;
}
}
if(!cunzai){
strcpy(yh[i].yqx[tot].ca,js[k].jqx[l].ca);
yh[i].yqx[tot].level=js[k].jqx[l].level;
tot++;
}
}
}
}
}
yh[i].to=tot;
// printf("tot=%d\n",tot);
// printf("\n");
}
/*check 取出的權限*/
/*
for(int i=0;i<u;i++){
printf("%s->",yh[i].user);
for(int j=0;j<yh[i].to;j++){
printf("%s:%d ",yh[i].yqx[j].ca,yh[i].yqx[j].level);
}printf("\n");
}
// */
scanf("%d",&q);
for(int i=0;i<q;i++){
char tmp[maxn],tmp1[maxn];
scanf("%s%s",tmp,tmp1);
/*取出權限和權限等級*/
int pos=-1;
char qxmc[maxn]="";
int qxdj=-1;
for(int j=0;j<strlen(tmp1);j++){
if(tmp1[j]==':'){
pos=j;
break;
}
}
if(pos==-1){
strcpy(qxmc,tmp1);
}else{
strncpy(qxmc,tmp1,pos);
qxdj=tmp1[pos+1]-'0';
}
// printf("qxmc=%s ,qxdj=%d\n",qxmc,qxdj);
bool f=false;
int userid;
for(int j=0;j<u;j++){
if(strcmp(yh[j].user,tmp)==0){
f=true;
userid=j;
break;
}
}
// printf("userid=%d \n",userid);
if(!f) printf("false\n");
else{
bool f1=false;
bool f2=false;
int ans;
for(int j=0;j<yh[userid].to;j++){
if(strcmp(yh[userid].yqx[j].ca,qxmc)==0){
// printf("*************\n");
if(qxdj==-1){
if(yh[userid].yqx[j].level==-1) f1=true;
else ans=yh[userid].yqx[j].level,f2=true;
}else{
// printf("level=%d\n",yh[userid].yqx[j].level);
if(qxdj<=yh[userid].yqx[j].level) f1=true;
}
// printf("qxdj=%d\n",qxdj);
}
}
if(f1) printf("true\n");
else if(f2) printf("%d\n",ans);
else printf("false\n");
}
}
return 0;
}