백준(1764) - 듣보잡 Python

최대 1 분 소요

백준(1764) - 듣보잡

문제풀이: Hash, 파이썬

해결방법

예시)

# 입력 #
3 4
ohhenrie
charlie
baesangwook
obama
baesangwook
ohhenrie
clinton

# 출력결과 #
2
baesangwook
ohhenrie
  • 이진탐색으로도 해결이 가능하지만 hash 알고리즘으로 푸는 방식이 시간효율이 더 좋다.
  • 파이썬에서는 dict를 사용하면 된다.
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
hashmap = {}
for _ in range(n):
    s = input().rstrip()
    if s not in hashmap:
        hashmap[s] = s

cnt = 0
names = []
for _ in range(m):
    s = input().rstrip()
    if s in hashmap:
        names.append(s)
        cnt += 1

names.sort()
print(cnt)
for name in names:
    print(name)

댓글남기기