본문 바로가기

# 소쿠리 개발 공부방/코테 문제풀이

[BAEKJOON] [10816] 숫자카드2: PYTHON

[10816] 숫자카드2

 

10816번: 숫자 카드 2

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

www.acmicpc.net

Input

  • 첫째 줄: 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)
  • 둘째 줄: 숫자 카드에 적혀있는 정수
  • 셋째 줄: M(1 ≤ M ≤ 500,000)
  • 넷째 줄: 상근이가 몇 개 가지고 있는 숫자 카드인지 구해야 할 M개의 정수

Output

  • M개의 수에 대해서, 각 수가 적힌 숫자 카드를 상근이가 몇 개 가지고 있는지를 공백으로 구분해 출력

 

✨ Solve

  • 세야 하는 숫자, 개수로 이루어진 dict 생성
  • for문 돌면서 개수 확인

 

💻 Code

### 언어 python3, 메모리 115656KB, 시간 1076ms

import sys

n = int(input())
cards = list(map(int, sys.stdin.readline().rstrip().split()))

m = int(input())
numbers = list(map(int, sys.stdin.readline().rstrip().split()))

num_dict = {}
for number in numbers:
    num_dict[number] = 0

for card in cards:
    try:
        if num_dict[card]>=0:
            num_dict[card] += 1
    except:
        ...

for number in numbers:
    print(num_dict[number], end=' ')