IT/ps
백준 11723번 집합
u149_cinderella
2025. 2. 8. 16:12
딱히 설명할게 없다 바로 코드 작성해보자
import sys
n=int(sys.stdin.readline().rstrip())
s={}
for _ in range(n):
input_op=sys.stdin.readline().rstrip().split()
if len(input_op)>1:
op_num=int(input_op[1])
op_code=input_op[0]
if op_code=='add':
s[op_num]=0
elif op_code=="remove":
if op_num in s:
s.pop(op_num)
elif op_code=="check":
if op_num in s:
print(1)
else:
print(0)
elif op_code=="toggle":
if op_num in s:
s.pop(op_num)
else:
s[op_num]=0
elif op_code=="all":
s={}
for i in range(1,21):
s[i]=0
elif op_code=="empty":
s={}
처음에 그냥 스택으로 작성하다가 remove보자마자 해시로 변경했다
해시로 푸는게 아닌듯하다
이럴때가 제일 난감하다 동일코드가 pypy로 될 때가 제일 머리아프다
근데 나는 이걸 비트마스킹으로 풀려고 온거기 때문에 다른 사람들이 작성한 코드를 봐보자
import sys
n=int(sys.stdin.readline().rstrip())
s=[0]*21
for _ in range(n):
input_op=sys.stdin.readline().rstrip().split()
if len(input_op)>1:
op_num=int(input_op[1])
op_code=input_op[0]
if op_code=='add':
s[op_num]=1
elif op_code=="remove":
s[op_num]=0
elif op_code=="check":
if s[op_num]==1:
print(1)
else:
print(0)
elif op_code=="toggle":
if s[op_num]==0:
s[op_num]=1
else:
s[op_num]=0
elif op_code=="all":
for i in range(1,21):
s[i]=1
elif op_code=="empty":
for i in range(1,21):
s[i]=0
이렇게 작성해줬다.
다른사람들은 비트연산자까지 사용하던데 나는 일단 이렇게 작성했다.
...
비트마스크로 다시 작성하자 왜 안되는지도 모르겠다
import sys
n=int(sys.stdin.readline().rstrip())
s=0
for _ in range(n):
input_op=sys.stdin.readline().rstrip().split()
if len(input_op)>1:
op_num=int(input_op[1])
op_bit=1<<(op_num-1)
op_code=input_op[0]
if op_code=='add':
s=s | op_bit
elif op_code=="remove":
s= s & (~op_bit)
elif op_code=="check":
if s & op_bit==op_bit:
print(1)
else:
print(0)
elif op_code=="toggle":
s=s^op_bit
elif op_code=="all":
s=(1<<20)-1
elif op_code=="empty":
s=0
수정완료
???
서버문제였다 채점현황 봐보니 전부 런타임에러로 도배가 되어있다
좀 기다렸다가 정상화되면 다시 제출해보자
아마 코드 자체는 정답인 듯
둘다 정답이 맞았다.