python | 숫자 카드 2 [백준 10816]

나경호·2022년 4월 9일
0

알고리즘 Algorithm

목록 보기
41/106

숫자 카드 2

출처 | 숫자 카드 2 [백준 10816]

문제

숫자 카드는 정수 하나가 적혀져 있는 카드이다. 상근이는 숫자 카드 N개를 가지고 있다. 정수 M개가 주어졌을 때, 이 수가 적혀있는 숫자 카드를 상근이가 몇 개 가지고 있는지 구하는 프로그램을 작성하시오.

입력

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

셋째 줄에는 M(1 ≤ M ≤ 500,000)이 주어진다. 넷째 줄에는 상근이가 몇 개 가지고 있는 숫자 카드인지 구해야 할 M개의 정수가 주어지며, 이 수는 공백으로 구분되어져 있다. 이 수도 -10,000,000보다 크거나 같고, 10,000,000보다 작거나 같다.

출력

첫째 줄에 입력으로 주어진 M개의 수에 대해서, 각 수가 적힌 숫자 카드를 상근이가 몇 개 가지고 있는지를 공백으로 구분해 출력한다.


풀이 1 [from collections import Counter]

from sys import stdin
from collections import Counter

N = int(stdin.readline())
arr = list(map(int, stdin.readline().split()))
M = int(stdin.readline())
data = list(map(int, stdin.readline().split()))

count = Counter(arr)

for i in range(M):
    print(count[data[i]], end = ' ')

풀이 2 [Hashmap; dictionary]

from sys import stdin

N = int(stdin.readline())
arr = list(map(int, stdin.readline().split()))
M = int(stdin.readline())
data = list(map(int, stdin.readline().split()))

counter = {}

for n in arr:
    if n in counter:
        counter[n] += 1
    else:
        counter[n] = 1

print(' '.join(str(counter[m]) if m in counter else '0' for m in data))

출처

알고리즘 분류

profile
기억창고👩‍🌾

0개의 댓글