Some pairs of permissions must never belong to the same person, such as creating a payment and approving it. A check over all users' roles finds the conflicts.
What to build
Find users whose roles together give them a forbidden pair of permissions.
Requirements
- Line 1: R. Then R lines
role perm1,perm2,.... Line R + 2: C. Then C forbidden pairspermA permB. Line R + C + 3: U. Then U linesuser role1,role2,.... - A user's permissions are the union of their roles' permissions (unknown roles give nothing).
- For each user, in input order, list every forbidden pair (in pair order) that they hold both halves of, as
user: permA+permB, .... - Users with no conflict are not printed. If nobody has a conflict, print
no conflicts.
Input
As described.
Output
As described.
Constraints
Up to 20 roles, 20 pairs and 50 users.
Example
Input:
2
clerk create-payment
manager approve-payment
1
create-payment approve-payment
2
rahim clerk
mina clerk,manager
Output:
mina: create-payment+approve-payment
Why it works
Conflicts often come from combinations of roles, each harmless alone, so the check must build each user's full set of permissions first and only then test every forbidden pair against it.
Examples
Example 1 · Input
2 clerk create-payment manager approve-payment 1 create-payment approve-payment 2 rahim clerk mina clerk,manager
Expected output
mina: create-payment+approve-payment
Example 2 · Input
1 viewer read 1 read write 1 x viewer
Expected output
no conflicts
Hints
Hint 1
Build a Map from role to a Set of permissions, then a Set per user.
Solution
Show one correct solution (try it yourself first)
const r = Number(prompt());
const roles = new Map();
for (let k = 0; k < r; k++) {
const [role, perms = ""] = prompt().trim().split(/\s+/);
roles.set(role, perms.split(",").filter(Boolean));
}
const c = Number(prompt());
const pairs = [];
for (let k = 0; k < c; k++) pairs.push(prompt().trim().split(/\s+/));
const u = Number(prompt());
let any = false;
for (let k = 0; k < u; k++) {
const [user, list = ""] = prompt().trim().split(/\s+/);
const perms = new Set();
for (const role of list.split(",").filter(Boolean)) for (const p of roles.get(role) ?? []) perms.add(p);
const hits = pairs.filter(([a, b]) => perms.has(a) && perms.has(b)).map(([a, b]) => `${a}+${b}`);
if (hits.length) {
any = true;
console.log(`${user}: ${hits.join(", ")}`);
}
}
if (!any) console.log("no conflicts");
▶ Open this solution in the compiler, then press Visualize to watch it run step by step