728x90
https://www.acmicpc.net/problem/1620
1620번: 나는야 포켓몬 마스터 이다솜
첫째 줄에는 도감에 수록되어 있는 포켓몬의 개수 N이랑 내가 맞춰야 하는 문제의 개수 M이 주어져. N과 M은 1보다 크거나 같고, 100,000보다 작거나 같은 자연수인데, 자연수가 뭔지는 알지? 모르면
www.acmicpc.net
시간초과 실패 try
리스트를 이용했고, 아래 딕션너리와 다르게 숫자 인덱스에 포켓몬 이름만 저장 가능하므로
포켓몬 이름이 주어졌을 때, 인덱스를 찾기 위해선 index()함수를 쓸 수 밖에 없다.. 이게 시간초과를 유발한다.
import sys
input = sys.stdin.readline
N, M = map(int,input().split())
lst = []
for i in range(N):
lst.append(input().rstrip())
for i in range(M):
a = input().rstrip()
if a.isdigit(): print(lst[int(a)-1])
else:print((lst.index(a))+1)
Sol
import sys
input = sys.stdin.readline
N, M = map(int,input().split())
dic = {}
for i in range(N):
a = input().rstrip()
dic[i+1] = a
dic[a] = i+1
for i in range(M):
b = input().rstrip()
if b.isdigit(): print(dic[int(b)])
else:print(dic[b])
'Baekjoon Algorithm > python' 카테고리의 다른 글
[Python]BAEKJOON 6491번 Perfection (0) | 2023.03.02 |
---|---|
[Python]BAEKJOON 11723번 집합 (0) | 2023.02.27 |
[Python]BAEKJOON 4335번 서로소 (0) | 2023.02.23 |
[Python]BAEKJOON 11689 번 GCD(n, k) = 1 (0) | 2023.02.23 |
[Python]BAEKJOON 11005번 진법 변환 2 (0) | 2023.02.23 |
댓글