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을 사용하여 번호 ↔ 이름을 대응시키는 문제임
- 정수 입력일 때:
- map<int, string>을 사용하여 번호 → 이름을 매핑
- 주어진 번호에 해당하는 포켓몬 이름을 빠르게 찾아 출력
- 문자열 입력일 때:
- map<string, int>을 사용하여 이름 → 번호를 매핑.
- 주어진 이름에 해당하는 포켓몬 번호를 빠르게 찾아 출력
느낀점
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
위 코드를 써서 입출력 성능을 최적화 하지 않으면 시간 초과 나옴..
'코딩테스트 공부 2025 Start > 1주차 (구현)' 카테고리의 다른 글
2559) 수열 (C++) (0) | 2025.04.09 |
---|---|
9996) 한국이 그리울 땐 서버에 접속하지 (0) | 2025.04.05 |
11655) ROT13 (0) | 2025.04.05 |
1159) 농구 경기 (문자열) (0) | 2025.03.30 |
10988) 팰린드롬인지 확인하기 (reverse, 투포인터) (0) | 2025.03.30 |