알고리즘/자료구조

[백준]나는야 포켓몬 마스터 이다솜 #dictionary

씩씩한 IT블로그 2020. 6. 16. 23:02
반응형

1. M에서 글자가 들어오면 index를 찾는 방식으로 구현했을때 시간초과가 발생.

import sys

N,M=map(int,input().split())
L=[]
for i in range(N):
    L.append(sys.stdin.readline().rstrip())
for i in range(M):
    temp=sys.stdin.readline().rstrip()

    try:
        temp=int(temp)-1
        print(L[temp])
    except:
        print(L.index(temp)+1)

 

2. dictionary를 이용하여 key를 찾는 형식으로 했을 때 통과

import sys

N,M=map(int,input().split())
dic1={}
dic2={}

for i in range(N):
    name=sys.stdin.readline().rstrip()
    dic1[name]=i+1
    dic2[i+1]=name
for i in range(M):
    temp=sys.stdin.readline().rstrip()
    try:
        print(dic2[int(temp)])
    except:
        print(dic1[temp])
반응형