Coding test
[백준/11723/파이썬] 집합
코드짜는쿤스트
2023. 1. 11. 11:48
소스코드 (시간초과가 났지만 맞는..)
M = int(input())
S = [0]*21
for i in range (M):
command = input().split()
if command[0] == 'add':
S[int(command[1])] = 1
if command[0] == 'remove':
S[int(command[1])] = 0
if command[0] == 'check':
print(S[int(command[1])])
if command[0] == 'toggle':
S[int(command[1])] = ~ S[int(command[1])]+2
if command[0] == 'all':
S = [1]*21
if command[0] == 'empty':
S = [0]*21
알고리즘
요즘 DP 문제를 풀다보니 거기에 꽂혀서 풀었는데 시간초과가 나버렸따..
그래서 집합 문제니깐 파이썬에 있는 set()을 활용하기로 했다.
소스코드
M = int(input())
S = set()
for _ in range(M):
temp = input().split()
if temp[0] == "all":
S = set([i for i in range(1, 21)])
elif temp[0] == 'empty':
S = set()
else:
func, x = temp[0], temp[1]
x = int(x)
if func == "add":
S.add(x)
elif func == "remove":
S.discard(x)
elif func == "check":
print(1 if x in S else 0)
elif func == "toggle":
if x in S:
S.discard(x)
else:
S.add(x)
알고리즘
간단한 집합 연산을 가지고 풀 수 있는 문제이다.
백준 가보면 정답률이 굉장히 낮은데 왜 낮은지는 잘 모르겠다.