[백준/python] 22233 가희와 키워드

박동현·2024년 8월 9일

Algorithm

목록 보기
1/11
post-thumbnail

1. 세트를 이용하는 방법

import sys
input = sys.stdin.readline
 
N,M = map(int,input().split())
data = set(input().strip() for _ in range(N))
 
for _ in range(M):
    arr = set(input().strip().split(","))
    data -= arr
    print(len(data))

2. 딕셔너리를 이용하는 방법

import sys
input = sys.stdin.readline

N,M = map(int,input().split())
data = {input().strip(): True for _ in range(N)}

for _ in range(M):
	arr = input().strip().split(",")
    for keyword in arr:
    	if data.get(keyword):
        	data[keyword] = False
            N -= 1
	print(N)           

결과

파이썬을 다루면서 딕셔너리를 우선적으로 생각하게 되어 바로 떠오르는 풀이는 2번이었으나, 딕셔너리의 값이 True/False 두 종류뿐이라면 이는 세트와 크게 다르지 않을 것이라는 생각에 세트로 풀이하는 방법 또한 작성했습니다.

세트로 풀이하는 경우, 마지막에 len을 거쳐 정답을 출력하기에 추가적인 비용이 발생할 것이라 생각했는데 오히려 걸린 시간이 더 짧아서 신기하긴 했습니다.

profile
동현

0개의 댓글