Algorithm/Baekjoon
[Baekjoon] 10816. 숫자 카드 2(Silver 4)[Python]
RIEN😚
2022. 6. 6. 19:36
728x90
반응형
10816번: 숫자 카드 2
첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,
www.acmicpc.net
문제
상근이는 숫자 카드 N개를 가지고 있습니다. 정수 M개가 주어졌을 때, 이 수가 적혀있는 숫자 카드를 상근이가 몇 개 가지고 있는지 구하는 프로그램을 작성하세요.
풀이
map을 이용하면 쉽게 풀 수 있습니다.
코드
import sys
input = sys.stdin.readline
n = int(input())
ns = list(map(int, input().split()))
m = int(input())
ms = list(map(int, input().split()))
nums = {}
for n in ns:
if n in nums:
nums[n] += 1
else:
nums[n] = 1
answer = ''
for m in ms:
answer += ('%d ' % ( nums[m] if m in nums else 0))
print(answer)
반응형