본문 바로가기
python-algorithm

백준 1620 나는야 포켓몬 마스터 이다솜

by 무적김두칠 2022. 11. 9.

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

 

1620번: 나는야 포켓몬 마스터 이다솜

첫째 줄에는 도감에 수록되어 있는 포켓몬의 개수 N이랑 내가 맞춰야 하는 문제의 개수 M이 주어져. N과 M은 1보다 크거나 같고, 100,000보다 작거나 같은 자연수인데, 자연수가 뭔지는 알지? 모르면

www.acmicpc.net

 

1
2
3
4
5
6
7
8
9
10
11
12
13
import sys
n , m =map(int,sys.stdin.readline().split())
pokemon = dict()
for i in range(1,n+1):
    name = sys.stdin.readline().rstrip()
    pokemon[name] = i
pokemon_reversed = dict(map(reversed, pokemon.items()))
for i in range(m):
    target=sys.stdin.readline().rstrip()
    if target.isnumeric():
        print(pokemon_reversed[int(target)])
    else:
        print(pokemon[target])
cs

딕셔너리 사용하면 되고
Line 7  처럼 index가 입력되는 경우를 위해 키, 벨류를 서로 바꾼 딕셔너리도 추가해줍니다
Using dictionary, In line 7 We need key and value changed dictionary for index of input. 
그리고 속도 문제로 인해서 input() 대신 sys.stdin.readline()을 사용하고 '\n' 과 같은 개행문자 를 지우기위해 .rstrip()을 사용합니다.
With time limit , Stead of input(), use sys.stdin.readline()

반응형

'python-algorithm' 카테고리의 다른 글

백준 11576 Base Conversion  (0) 2022.11.10
백준 18258 큐 2  (0) 2022.11.09
백준 4949 균형잡힌 세상  (0) 2022.11.09
백준 18398 HOMWRK  (0) 2022.11.09
백준 1966 프린터 큐  (0) 2022.11.08

댓글