알고리즘/백준
[Python] 백준 파이썬 10816 숫자 카드 2
dding96
2022. 9. 29. 07:45
https://www.acmicpc.net/problem/10816
10816번: 숫자 카드 2
첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,
www.acmicpc.net
# 백준 10816 숫자 카드 2
from collections import Counter
n = int(input())
hand_card = [n for n in map(int, input().split())]
card_counter = Counter(hand_card)
m = int(input())
check_list = [n for n in map(int, input().split())]
for i in check_list:
if i in card_counter:
print(card_counter[i], end=' ')
else:
print(0, end=' ')
Python에서 제공하는 Counter()라는 함수가 있습니다.
만약에 a = [1, 2, 3, 3, 1, 2] 라는 리스트가 있고 count = Counter(a)를 해주면 아래와 같은 모양의 딕셔너리를 반환합니다.
count = {1 : 2, 2 : 2, 3 : 2}
각 숫자별 빈도수를 딕셔너리로 반환해줍니다.
이걸 이용하면 쉬운 문제인 것 같습니다.