본문 바로가기
코딩테스트 공부 2025 Start/1주차 (구현)

1620) 나는야 포켓몬 마스터 이다솜 (C++)

by xortl98 2025. 4. 10.
728x90

 출처 

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

 

 내 풀이 

더보기
더보기
#include<iostream>
#include<map>
#include<string>

using namespace std;

map<string, string> pokemons;
map<int, string>pokemon_numbers;

int N, M;
string pokemon;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> N >> M;

	for (int i = 1; i <= N; i++)
	{
		cin >> pokemon;
		pokemons.insert({ pokemon, to_string(i) });
		pokemon_numbers.insert({ i, pokemon });
	}

	for (int i = 0; i < M; i++)
	{
		cin >> pokemon;

		if (pokemon[0] >= '1' && pokemon[0] <= '9')
		{
			cout << pokemon_numbers[stoi(pokemon)] << '\n';
		}
		else cout << pokemons[pokemon] << '\n';
	}
}

 

 해설

이 문제는 두 개의 map을 사용하여 번호 ↔ 이름을 대응시키는 문제임 

  1. 정수 입력일 때:
    • map<int, string>을 사용하여 번호 → 이름을 매핑
    • 주어진 번호에 해당하는 포켓몬 이름을 빠르게 찾아 출력
  2. 문자열 입력일 때:
    • map<string, int>을 사용하여 이름 → 번호를 매핑.
    • 주어진 이름에 해당하는 포켓몬 번호를 빠르게 찾아 출력

 

 느낀점 

	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

위 코드를 써서 입출력 성능을 최적화 하지 않으면 시간 초과 나옴..