본문 바로가기
TIL - 프로그래밍/Python 알고리즘

[백준] 1764. 듣보잡 - Python

by chaemj97 2022. 6. 11.
728x90

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

 

1764번: 듣보잡

첫째 줄에 듣도 못한 사람의 수 N, 보도 못한 사람의 수 M이 주어진다. 이어서 둘째 줄부터 N개의 줄에 걸쳐 듣도 못한 사람의 이름과, N+2째 줄부터 보도 못한 사람의 이름이 순서대로 주어진다.

www.acmicpc.net


  • 시간
    • import 사용 코드 시간이 30배이상 걸렸다...
      • 시간이 오래 걸려도 통과는 한다.
    • sys.stdin을 쓰도록 습관을 들여야겠다.


  • 코드1 - import
# 듣도 못한 사람 N, 보도 못한 사람 M
N,M = map(int,input().split())
nohear = set(input() for _ in range(N))
nosee = set(input() for _ in range(M))

# nohear과 nosee의 공통 찾기
no = nohear&nosee

# 듣보잡의 수
print(len(no))
# 듣보잡 명단(사전식 정렬)
for n in sorted(no):
    print(n)
  • 코드2 - sys.stdin
from sys import stdin

# 듣도 못한 사람 N, 보도 못한 사람 M
N,M = stdin.readline().rstrip().split()
nohear = set(stdin.readline().rstrip() for _ in range(int(N)))
nosee = set(stdin.readline().rstrip() for _ in range(int(M)))

# nohear과 nosee의 공통 찾기
no = nohear&nosee

# 듣보잡의 수
print(len(no))
# 듣보잡 명단(사전식 정렬)
for n in sorted(no):
    print(n)
728x90
반응형

댓글