728x90
# 백준 집합과 맵 : 10816번 - 숫자 카드 2
# 딕셔너리 이용
# 가지고 있는 숫자 카드 개수 n
n = int(input())
# n개의 숫자 카드에 적혀있는 정수 cards 리스트에 넣기
cards = list(map(int, input().split(' ')))
# 가지고 있는 숫자 카드와 비교할 개수 m
m = int(input())
# 가지고 있는 숫자 카드인지 구해야 할 m개의 정수
guess = list(map(int, input().split(' ')))
# 몇 개 가지고 있는 비교하기
ans = {}
for card in cards:
if card in ans:
ans[card] += 1
else:
ans[card] = 1
# 결과 출력하기
for i in guess:
if ans.get(i) == None:
print(0, end=' ')
else:
print(ans.get(i), end=' ')
처음에 시간초과났었다.
알고보니 딕셔너리를 이용해서 푸는 거였다.
'Algorithm > Baekjoon' 카테고리의 다른 글
[백준 2557] C++ - Hello World!😄 (0) | 2024.07.02 |
---|---|
#백준 1764, 파이썬 / set 집합 자료형 교집합, 합집합, 차집합 (0) | 2022.10.20 |
#백준 1620, 파이썬 / sys.stdin.readline (0) | 2022.10.06 |
#백준 14425, 파이썬 (0) | 2022.10.06 |
#백준 11659, 파이썬, PyPy3와 Python3 차이점 (0) | 2022.09.27 |