알고리즘 공부
백준 1620번 dictionary의 활용
transfer_kk
2023. 2. 28. 12:27
문제
https://www.acmicpc.net/problem/1620
1620번: 나는야 포켓몬 마스터 이다솜
첫째 줄에는 도감에 수록되어 있는 포켓몬의 개수 N이랑 내가 맞춰야 하는 문제의 개수 M이 주어져. N과 M은 1보다 크거나 같고, 100,000보다 작거나 같은 자연수인데, 자연수가 뭔지는 알지? 모르면
www.acmicpc.net
나의 풀이
import sys
pocketmonCount, problemCount = map(int,input().split())
encyclopedia = dict()
for i in range(1, pocketmonCount+1):
encyclopedia[sys.stdin.readline().rstrip()] = i
convertEncyclopedia =dict(map(reversed,encyclopedia.items()))
for j in range(problemCount):
inputString = sys.stdin.readline().rstrip()
if inputString.isdigit():
print(convertEncyclopedia.get(int(inputString)))
else :
print(encyclopedia[inputString])
다른 사람의 풀이
import sys; input = sys.stdin.readline
N, M = map(int, input().split())
pokemon = dict()
for i in range(1, N + 1):
name = input().strip()
pokemon[i] = name
pokemon[name] = i
for _ in range(M):
quiz = input().strip()
if quiz.isdigit():
print(pokemon[int(quiz)])
else:
print(pokemon[quiz])
한 딕셔너리에 필요한 두 가지 값을 넣으므로서 굳이 딕셔너리를 두개 만들지 않았으며 딕셔너리의 key와 value 값을 바꾸는 번거로운 일도 하지 않았다.
딕셔너리의 활용 복습
=> https://transfer-kk.tistory.com/29
[파이썬] dictionary의 활용
목차 0. 딕셔너리란? 1. 딕셔너리 만들기 2. 딕셔너리에 key 값과 value값 할당하기 3.. key값으로 value값 출력하기 4. value값으로 key값 출력하기 5. in, not in 활용하여 key값 찾기 6. 딕셔너리의 key 값만 출
transfer-kk.tistory.com