首页 > 其他分享 >CSP201612-3权限查询

CSP201612-3权限查询

时间:2023-02-03 17:02:05浏览次数:44  
标签:string int myp cin 查询 ss vector CSP201612 权限

 

 

 

 


 

 

 

 

多年后再回头看这道题觉得很简单,写起来还是很复杂,我的书写习惯不好,找bug找了很久。

特别注意在构建角色时,一个角色可能会有多个权限,取最大值,又要与无权限的-1区别对待。

#include<bits/stdc++.h>
#define N 10001
#define testa
using namespace std;
int n,temp,x,y,m,mmm;
int p,r,u,q;
string s,ss,sss;
long long ans=2000000000;
short mapp[N][N];
int in[N],out[N];
vector<int> l;
unordered_map<string,int> privilege;
class role{
public:
    string name;
    unordered_map<string,int> myp;
    role(string na,vector<string> v){
        name=na;
        myp.clear();
        for(auto k:v){
            //cout<<k<<endl;
            int nnnn=k.find(':');
            //cout<<nnnn<<endl;
            if(nnnn==k.npos){
                sss=k;
                mmm=-1;
            }else{
                mmm=atoi(k.substr(nnnn+1).c_str());

                sss=k.substr(0,nnnn);
            }
            //cout<<"level "<<mmm<<endl;
            if(mmm==-1){
                this->myp[sss]=mmm;
            }else this->myp[sss]=max(this->myp[sss],mmm);//important
        }
#ifdef test
        for(auto qwer:this->myp){
            cout<<"in the role "<<na<<"  "<<qwer.first<<"  "<<qwer.second<<endl;
        }
#endif
    }
    //role(){}
};







//role newrole;
vector<role>rolelist;
unordered_map<string,int>rolemap;



class user{
public:
    string name;
    unordered_map<string,int> myuserp;
    user(string na,vector<string> v){
        name=na;
        for(auto k:v){
            //cout<<k<<endl;
            int roleid=rolemap[k];
            role r=rolelist[roleid];
            for(auto qwe:r.myp){
                if(myuserp.count(qwe.first)!=0){
                    myuserp[qwe.first]=max(myuserp[qwe.first],qwe.second);
#ifdef test
                    cout<<na<<" get a new p named "<<qwe.first<<" level "<<qwe.second<<" from role "<<r.name<<endl;
                    cout<<"this is update\n";
                    cout<<roleid<<endl;
#endif
                }else {
                    myuserp[qwe.first] = qwe.second;//???
#ifdef test
cout<<na<<" get a new p named "<<qwe.first<<" level "<<qwe.second<<" from role "<<r.name<<endl;
cout<<roleid<<endl;
#endif
                }
            }
        }
    }
    //role(){}
};
vector<user> userlist;
unordered_map<string,int>usermap;


int main(){
    cin>>p;
    for(int i=0;i<p;i++){
        cin>>s;
        n=s.find(':');

        if(n==s.npos){
            y=-1;
        }else{
            y=atoi(s.substr(n+1).c_str());
            s=s.substr(0,n);
        }
        privilege[s]=y;
    }
    cin>>r;
    for(int i=0;i<r;i++){
        cin>>s;
        cin>>temp;
        vector<string> q;
        for(int jj=0;jj<temp;jj++){
            cin>>ss;
            q.push_back(ss);

        }
        role newrole=role(s,q);
        rolelist.push_back(newrole);
        rolemap[s]=rolelist.size()-1;
#ifdef test
        //cout<<rolelist.size()-1;
#endif
    }
#ifdef test
    for(auto l:rolemap){
        cout<<"inrolemap";
        cout<<l.first<<' '<<l.second<<endl;
    }
#endif
    cin>>u;
    for(int i=0;i<u;i++){
        cin>>s;
        cin>>temp;
        vector<string> q;
        for(int jj=0;jj<temp;jj++){
            cin>>ss;
            q.push_back(ss);
        }
        user newuser=user(s,q);
        userlist.push_back(newuser);
        usermap[s]=i;
    }
    cin>>q;
    vector<int>ans;
    for(int i=0;i<q;i++){
        cin>>s;
        cin>>ss;
        if(usermap.count(s)==0){
#ifdef test
            cout<<"not found "<<s<<endl;
#endif
            ans.push_back(-2);
            continue;
        }
        user thisuser=userlist[usermap[s]];

        int iop=ss.find(':');
        if(iop!=ss.npos){
            int level=atoi(ss.substr(iop+1).c_str());
            ss=ss.substr(0,iop);
            if(thisuser.myuserp.count(ss)==0){
#ifdef test
                cout<<"no priv "<<ss<<endl;
#endif
                ans.push_back(-2);
                continue;
            }else{
                if(thisuser.myuserp[ss]<level){
#ifdef test
                    cout<<"priv too low "<<ss<<"  "<<level<<endl;
#endif
                    ans.push_back(-2);
                    continue;
                }
#ifdef test
                cout<<"priv too correct "<<ss<<endl;
#endif
                ans.push_back(-1);
                continue;

            }

        }else{
            if(thisuser.myuserp.count(ss)==0){
#ifdef test
                cout<<"no priv "<<ss<<endl;
#endif
                ans.push_back(-2);
                continue;
            }else{
                if(thisuser.myuserp[ss]==-1) {
#ifdef test
                    cout<<"yes priv "<<ss<<endl;
#endif
                    ans.push_back(-1);
                    continue;
                }
                ans.push_back(thisuser.myuserp[ss]);
                continue;

            }
        }


    }
    for(int i:ans){
        if(i==-2){
            cout<<"false\n";
        }else if(i==-1){
            cout<<"true\n";
        }else{
            cout<<i<<endl;
        }
    }
}

  

标签:string,int,myp,cin,查询,ss,vector,CSP201612,权限
From: https://www.cnblogs.com/hyffff/p/17089825.html

相关文章

  • MyBatis使用四(查询详解)
    本文主要讲述如何在mybatis中进行查询操作【详解】一.查询User对象1.查询单个对象UserSelectUser接口声明如下//主要条件是使用idpublicinterfaceSelect......
  • 根据ip查询归属地工具类
    publicstaticStringgetAddrByIp(Stringip){if(StrUtil.isBlank(ip)){returnnull;}Stringurl="https://api01.aliyun.venuscn.com/ip?ip="+......
  • linux查询磁盘是否做raid
    1、查看是否安装mdadm包rpm-qa|grepmdadm2、查看磁盘是否做了raidmdadm-E/dev/sd[bcde]1#可以忽略/dev/sda,一般都是用作系统盘mdadm:Nomdsuperblockdetectedon/d......
  • C++ 哈希表查询_进入哈希函数结界的世界
    1.前言哈希表或称为散列表,是一种常见的、使用频率非常高的数据存储方案。哈希表属于抽象数据结构,需要开发者按哈希表数据结构的存储要求进行API定制,对于大部分高级语言......
  • 笔记:海量数据的查询方法
    概述:每年大约有几千万近一亿的业务数据量,如何提高查询性能。具体方案:在表结构初始化阶段时,需要添加查询条件的索引;并且可以使用uuid主键和数字主键的联合业务主键,根据......
  • 误删除系统超级用户(superuser)权限的恢复方式
    在使用KingbaseES数据库的时候,系统默认存在一个跟系统初始化用户同名的Superuser(默认是system用户,可更改)。这个Superuser的存在其实对于权限的管控是很有用的,但是如果我们......
  • 分页查询
    先添加maven依赖<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>4.2.0</version></dependency><d......
  • 关于Spring Data Jpa的分页、跨表查询
    分页jqa的query使用原生sql查询​​(nativeQuery=true)​​​的时候不支持排序的,需要在sql里面加上:​​ORDERBY?#{#pageable}​​​​SpringDataJpa本地查询(带分页方式......
  • 简易数字电压表+ADC0809+程序查询(延时等待)方式
    1实验现象2实验原理3系统设计4硬件设计5软件设计5.1主程序#include<REG52.H>#include"DisplaySmg.h"#include"ADC0809.h"#include"Timer0.h"#incl......
  • sql server 2008 R2在查询分析器执行查询某个表时一直在运行没有返回结果的原因
    错误表象:软件的材料订单打开时一直加载中,如图:  调试程序,发现执行到AB框架ABThirdDBU单元的2010行:ifABGetDatasetSQL(aDataSet)<>EmptyStrthenaDataSet.......