[백준] 1620 포켓몬 마스터

cheeeese·2022년 4월 26일
0

코딩테스트 연습

목록 보기
90/151
post-thumbnail

📖 문제

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

💻 내 코드

import sys

n, m=map(int, sys.stdin.readline().split())
poket={}

for i in range(1,n+1):
    name=sys.stdin.readline().strip()
    poket[i]=name
    poket[name]=i
    
for _ in range(m):
    quest=sys.stdin.readline().strip()
    if quest.isalpha():
        print(poket[quest])
    else:
        print(poket[int(quest)])

💡 풀이

  • 처음엔 리스트에 이름을 저장하고 출력할 때 인덱스로 찾거나 이름으로 찾는 방법으로 했는데 시간 초과가 뜸

처음 제출했던 코드

import sys

n, m=map(int, sys.stdin.readline().split())
poket=[]

for i in range(n):
    poket.append(sys.stdin.readline().strip())

for i in range(m):
    quest=input()
    if quest.isalpha():
        print(poket.index(quest)+1)
    else:
        print(poket[int(quest)-1])
  • dictionary를 사용해서 번호와 이름을 같이 저장해줌

0개의 댓글