A service stores a user's permissions in a single integer (the permission value).
The first line gives the number of permissions k, and the second line gives k permission names separated by single spaces.
Call them permission 0, 1, …, k-1 from the front; permission i corresponds to the value 2^i.
When the permission value is written in binary, the user has permission i if the 2^i digit is 1, and does not if it is 0.
For example, with permissions read write delete admin in that order, the permission value 5 (= 1 + 4) has read and delete.
The third line gives the number of queries q, and each of the next q lines gives one query.
A query is a permission value, a mode and one or more permission names, separated by single spaces.
With mode "all" the query passes if the user has every listed permission, with "any" if they have at least one, and with "none" if they have none of them.
For each query print "yes" if it passes, otherwise "no", on its own line.
Input
1 ≤ k ≤ 30, permission names are 1 to 10 lowercase letters and all different, 1 ≤ q ≤ 1,000, the mode is one of all, any, none, 0 ≤ permission value < 2^k, the names in a query are in the list and all different
Output
one line "yes" or "no" per query
Example 1 · stdin
4
read write delete admin
5
5 all read delete
5 all read write
2 any read delete
2 any write admin
8 none read write
stdout
yes
no
no
yes
yes
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
The value 2^i of permission i is exactly the shift 1 << i in C, C++, Java, C#, JS, TS and Python. Python 2 ** i and Java/C# Math.pow/Math.Pow (which return a double) work too, but for powers of two 1 << i stays an integer and is the cleanest. 2 ^ i is bitwise XOR, not a power (2 ^ 0 = 2, 2 ^ 1 = 3), so it checks the wrong permission without any error. Craykon has no bit operators and no power operator, so build the table from 1 by doubling with value *= 2, and test a bit with Mathf.Quot(mask, value) % 2 == 1 (the quotient by 2^i is odd). Operator-precedence trap: in C, C++, JS and TS mask & bit != 0 evaluates != first and becomes mask & 1, giving a wrong answer. Always write (mask & bit) != 0 with parentheses. In Java and C# the same expression is a compile error for mixing int and bool, so you notice at once; in Python & binds tighter than comparisons, so it is right even without parentheses. Boolean trap: C and C++ treat any nonzero integer as true, but Java and C# reject if (mask & bit) and need a real boolean. C has bool from <stdbool.h>, Craykon has true/false. Collect "all" starting from true with &&, and "any" starting from false with ||. Python all/any and JS/TS every/some do the same. "none" is "not any", not "not all"; the two differ for a user who has only some of the permissions. 1 << i is a 32-bit int, so for i = 31 it is negative, and Java, C#, JS and TS use only the shift count modulo 32, so 1 << 32 is 1 (in C and C++ it is undefined behavior). More than 32 permissions would need 64-bit integers (1L << i, 1LL << i, BigInt in JS/TS), but this problem keeps k ≤ 30.
Pythonmain.py · 15 lines
k = int(input())names = input().split()bit_of = {name: 1 << i for i, name in enumerate(names)} # i번째 권한의 값은 2^iq = int(input())for _ in range(q): t = input().split() mask, mode, wanted = int(t[0]), t[1], t[2:] has = [mask & bit_of[w] != 0 for w in wanted] # Python은 &가 !=보다 먼저 계산된다 if mode == "all": ok = all(has) elif mode == "any": ok = any(has) else: # none ok = not any(has) print("yes" if ok else "no")
JavaScriptmain.js · 16 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const names = lines[1].trim().split(/\s+/);const bitOf = new Map(names.map((name, i) => [name, 1 << i])); // i번째 권한의 값은 2^iconst q = Number(lines[2]);const out = [];for (let j = 0; j < q; j++) { const [m, mode, ...wanted] = lines[3 + j].trim().split(/\s+/); const mask = Number(m); const has = wanted.map((w) => (mask & bitOf.get(w)) !== 0); // 괄호 필수: !==가 &보다 먼저다 let ok; if (mode === "all") ok = has.every((h) => h); else if (mode === "any") ok = has.some((h) => h); else ok = !has.some((h) => h); // none out.push(ok ? "yes" : "no");}console.log(out.join("\n"));
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;public class Main { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int k = Integer.parseInt(in.readLine().trim()); String[] names = in.readLine().trim().split("\\s+"); Map<String, Integer> bitOf = new HashMap<>(); for (int i = 0; i < k; i++) bitOf.put(names[i], 1 << i); // i번째 권한의 값은 2^i int q = Integer.parseInt(in.readLine().trim()); StringBuilder out = new StringBuilder(); for (int j = 0; j < q; j++) { String[] t = in.readLine().trim().split("\\s+"); int mask = Integer.parseInt(t[0]); String mode = t[1]; boolean all = true, any = false; for (int w = 2; w < t.length; w++) { boolean has = (mask & bitOf.get(t[w])) != 0; // Java는 int를 참·거짓으로 쓸 수 없어 != 0이 꼭 필요하다 all = all && has; any = any || has; } boolean ok = mode.equals("all") ? all : mode.equals("any") ? any : !any; out.append(ok ? "yes" : "no").append('\n'); } System.out.print(out); }}
C#Main.cs · 32 lines
using System;using System.Collections.Generic;using System.Text;class Program{ static void Main() { int k = int.Parse(Console.ReadLine()); string[] names = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); var bitOf = new Dictionary<string, int>(); for (int i = 0; i < k; i++) bitOf[names[i]] = 1 << i; // i번째 권한의 값은 2^i int q = int.Parse(Console.ReadLine()); var sb = new StringBuilder(); for (int j = 0; j < q; j++) { string[] t = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); int mask = int.Parse(t[0]); string mode = t[1]; bool all = true, any = false; for (int w = 2; w < t.Length; w++) { bool has = (mask & bitOf[t[w]]) != 0; // C#은 int를 bool로 쓸 수 없어 != 0이 꼭 필요하다 all = all && has; any = any || has; } bool ok = mode == "all" ? all : mode == "any" ? any : !any; sb.Append(ok ? "yes" : "no").Append('\n'); } Console.Write(sb); }}
C++main.cpp · 34 lines
#include <iostream>#include <sstream>#include <string>#include <unordered_map>int main() { int k; std::cin >> k; std::unordered_map<std::string, int> bitOf; for (int i = 0; i < k; i++) { std::string name; std::cin >> name; bitOf[name] = 1 << i; // i번째 권한의 값은 2^i } int q; std::cin >> q; std::string line; std::getline(std::cin, line); // q 뒤에 남은 줄바꿈 for (int j = 0; j < q; j++) { std::getline(std::cin, line); std::istringstream ss(line); int mask; std::string mode, w; ss >> mask >> mode; bool all = true, any = false; while (ss >> w) { bool has = (mask & bitOf[w]) != 0; // 괄호 필수: != 가 & 보다 먼저 계산된다 all = all && has; any = any || has; } bool ok = mode == "all" ? all : mode == "any" ? any : !any; std::cout << (ok ? "yes" : "no") << '\n'; }}
Cmain.c · 32 lines
#include <stdbool.h>#include <stdio.h>#include <string.h>int main(void) { int k; char names[30][11]; scanf("%d", &k); for (int i = 0; i < k; i++) scanf("%10s", names[i]); int q; scanf("%d", &q); for (int j = 0; j < q; j++) { int mask; char mode[5]; scanf("%d %4s", &mask, mode); bool all = true, any = false; /* 이름들은 줄 끝까지 이어진다: 다음 글자가 줄바꿈이면 멈춘다 */ int c; while ((c = getchar()) == ' ') { char w[11]; scanf("%10s", w); int i = 0; while (strcmp(names[i], w) != 0) i++; bool has = (mask & (1 << i)) != 0; /* 괄호 필수: != 가 & 보다 먼저 계산된다 */ all = all && has; any = any || has; } bool ok = strcmp(mode, "all") == 0 ? all : strcmp(mode, "any") == 0 ? any : !any; puts(ok ? "yes" : "no"); } return 0;}
Craykonmain.cray · 44 lines
// 크레이콘에는 비트 연산이 없다: mask를 2^i로 나눈 몫이 홀수이면 i번째 비트가 켜져 있다켜졌나(mask, 값){ return Mathf.Quot(mask, 값) % 2 == 1;}k = Console.ReadLine().ToNumber();names = Console.ReadLine().Split();bitOf = Dict();값 = 1;for (i = 0; i < k; i++){ bitOf[names[i]] = 값; // i번째 권한의 값은 2^i: 1에서 시작해 두 배씩 값 *= 2;}q = Console.ReadLine().ToNumber();for (j = 0; j < q; j++){ t = Console.ReadLine().Split(); mask = t[0].ToNumber(); mode = t[1]; 모두 = true; 하나라도 = false; for (w = 2; w < t.Count; w++) { 있음 = 켜졌나(mask, bitOf[t[w]]); 모두 = 모두 && 있음; 하나라도 = 하나라도 || 있음; } ok = false; if (mode == "all") { ok = 모두; } else if (mode == "any") { ok = 하나라도; } else { ok = !하나라도; } Console.WriteLine(ok ? "yes" : "no");}