11723-s5.py 성공

This commit is contained in:
sm4640
2026-01-20 23:25:50 +09:00
parent cefc086b08
commit 2d2a6f36fb

72
workbook_8708/11723-s5.py Normal file
View File

@@ -0,0 +1,72 @@
# 집합
import sys
input = sys.stdin.readline
s = {}
all_s = {i: 1 for i in range(1,21)}
def add(x):
if s.get(x, None) != None:
return
s[x] = 1
def remove(x):
if s.get(x, None) == None:
return
s.pop(x)
def check(x):
print(s.get(x, 0))
def toggle(x):
if s.get(x, None) != None:
s.pop(x)
else:
s[x] = 1
def all():
global s
s = all_s.copy()
def empty():
global s
s = {}
def excute(exc, *arg):
if exc == "add":
add(arg[0])
elif exc == "remove":
remove(arg[0])
elif exc == "check":
check(arg[0])
elif exc == "toggle":
toggle(arg[0])
elif exc == "all":
all()
elif exc == "empty":
empty()
else:
return
def solution():
global s
m = int(input())
for _ in range(m):
q = input().rstrip().split()
exc, arg = q[0], list(map(int, q[1:]))
excute(exc, *arg)
return
solution()
"""
걸린 시간: 30분(연산 함수를 OCP로 어떻게 할지 고민하다가 그냥 했는데 나중에는 디스패치 패턴 써야겠다..)
시간복잡도: O(m)
해설: set을 써도 되긴 하는데 너무 문제가 set 그 자체라서 dictionary로 구현해보았다.
x가 1에서 20까지니까 비트마스크 20자리로 해도 가능하다고 한다.
"""