Python实现201612-3权限查询

题目:http://118.190.20.162/view.page?gpid=T50

写了一个小时才做完,一开始拿了50分,后来拿了80,然后越调试越低,才发现方向错了

从80分调试到100分

  1. 通过查看题目意思,又看了看样例说明
评测用例规模与约定
  评测用例规模:
  * 1 ≤ p, r, u ≤ 100
  * 1 ≤ q ≤ 10 000
  * 每个用户具有的角色数不超过 10,每种角色具有的权限种类不超过 10
  约定:
  * 输入保证合法性,包括:
  1) 角色对应的权限列表(R 段)中的权限都是之前(P 段)出现过的,权限可以重复出现,如果带等级的权限重复出现,以等级最高的为准
  2) 用户对应的角色列表(U 段)中的角色都是之前(R 段)出现过的,如果多个角色都具有某一分等级权限,以等级最高的为准
  3) 查询(Q 段)中的用户名和权限类名不保证在之前(U 段和 P 段)出现过
  * 前 20% 的评测用例只有一种角色
  * 前 50% 的评测用例权限都是不分等级的,查询也都不带等级

重要

1) 角色对应的权限列表(R 段)中的权限都是之前(P 段)出现过的,权限可以重复出现,如果带等级的权限重复出现,以等级最高的为准
2) 用户对应的角色列表(U 段)中的角色都是之前(R 段)出现过的,如果多个角色都具有某一分等级权限,以等级最高的为准

然后想起来自己每次获取数值都是直接用等号,那么如果以前赋值的大,现在赋值的小,就会有错误,解决方法

a = max(a,b)
# 由于存储结构采用的dict,所以可以使用get方法获取某个key的值,改写上面的代码
a = dic.get(key, -1) #如果dic有key则返回value,否则返回-1
a = max(a,b)

完整代码如下

p = int(input())
# category hight level
category = {}
# user role
role = {}
user = {}
for i in range(p):
        line = input().split(':')
        if len(line) >= 2:
                category[line[0]] = category.get(line[0],0) + int(line[-1])
        else:
                category[line[0]]=-1
r = int(input())
for i in range(r):
        line = input().split(' ')
        role[line[0]]={}
        for s in line[2:]:
                # role is dic
                temp_privilege  = s.split(':')
                if len(temp_privilege)>=2:
                        role[line[0]][temp_privilege[0]] = max(role[line[0]].get(temp_privilege[0], -1)  , int(temp_privilege[-1]))
                else:
                        if temp_privilege[0] not in role[line[0]].keys():
                                role[line[0]][temp_privilege[0]] = -1

                
u = int(input())
for i in range(u):
        line = input().split(' ')
        user[line[0]]={}
        for s in line[2:]:
                for q in role[s]:
                        user[line[0]][q] = max(user[line[0]].get(q,-1) ,role[s].get(q, -1)) 
q = int(input())
for i in range(q):
        line = input().split(' ')
        is_print=False
        
        #  search privilege and rank
        if ':' in line[-1] and line[0] in user.keys():
                temp_role, temp_rank = line[-1].split(':')
                if user[line[0]].get(temp_role,-1) >= int(temp_rank):
                        is_print = True
        # only search privilege
        elif  line[0] in user.keys():
                if line[-1] in user[line[0]].keys():
                        if user[line[0]].get(line[-1],-1) >= 0:
                                print(user[line[0]][line[-1]])
                                continue
                        is_print = True
        if is_print:
                print("true")
        else:
                print("false")
        

posted @ 2020-08-15 22:06  Adam_lxd  阅读(146)  评论(0编辑  收藏  举报