CCF 201612-03 权限查询 题解

来源:互联网 发布:日本原单淘宝店 编辑:程序博客网 时间:2024/06/05 08:45

题目链接,注册了自己看把,那个认证题库已经好几天打不开了,没办法上链接了

中等模拟把,就是三层结构体嵌套。捋清楚就行。

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;}

原创粉丝点击