← 返回 snowflake 的题目列表Role Privileges Inheritance with Granting and Blocking Conflicts
类型:online_judge
snowflake
Role Privileges Inheritance and Conflict Resolution
In a privilege management system, you can assign privileges (e.g., "A", "B", "C", etc.) to a role or explicitly block privileges to grant a role certain access capabilities. Simultaneously, a role (the "granted" role) can be bestowed to another role (the "grantee" role), and the grantee role can inherit the privileges that the granted role is allowed.
Given a role hierarchy tree, with a total of n roles numbered from 0 to n-1. An array grants, where grants[i] = [fromi, toi] indicates there is a grant from role fromi to role toi. Each role has an allowed privilege list, allowedList[i] = [...] represents role i's allowed privileges, and a disallowed privilege list, disallowedList[i] = [...] represents role i's disallowed privileges. Privileges in the system are represented by the letters A to Z. Return a list answer, where answer[i] is the allowed privileges of role i.
Input
n: Total number of roles, an integer (1 ≤ n ≤ 1000)
grants: A 2D array representing privilege granting relationships
allowedList: A 2D character array of allowed privileges
disallowedList: A 2D character array of disallowed privileges
Output
A string array, where each element is the allowed privileges string of role i
Example
{
"n": 3,
"grants": [[0, 1], [1, 2]],
"allowedList": ["A", "BC", ""],
"disallowedList": ["", "", "B"]
}
Output:
["A", "ABC", "AC"]
Role 0 has privilege A. Role 1, being a grantee of role 0, inherits A and has additional BC, forming A + BC. Role 2 inherits from Role 1 and removes B, resulting in AC.
Example
Input
{"n": 3, "grants": [[0, 1], [1, 2]], "allowedList": ["A", "BC", ""], "disallowedList": ["", "", "B"]}