본문 바로가기

개발/algorithm

[백준 10816번] 숫자 카드 2 - python

문제 링크

https://www.acmicpc.net/problem/10816

 

10816번: 숫자 카드 2

첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,

www.acmicpc.net

풀이 

- defaultdict(int)으로 풀었다 

- 숫자가 key, 숫자의 등장개수가 value 

- 숫자 카드마다 등장하면 value를 1 씩 증가 

- 숫자 카드 별 value 출력, 등장하지 않았으면 0으로 초기화되어 출력됨 

from collections import defaultdict

n = int(input())
num = defaultdict(int)

data1 = list(map(int,input().split()))

for i in data1:
  num[i] += 1 

m = int(input())

data2 = list(map(int,input().split()))

for i in data2:
  print(num[i],end = " ")