![[백준 C++] 1620: 나는야 포켓몬 마스터 이다솜](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbgP8la%2FbtsMtbto3BC%2F8KZ9hdHAZEHmN9pF0ksMkk%2Fimg.png)
[백준 C++] 1620: 나는야 포켓몬 마스터 이다솜CSE/코딩 문제풀이2025. 2. 21. 18:44
Table of Contents
https://www.acmicpc.net/problem/1620
시간초과에 걸리지 않기 위해서 해시 맵을 사용해야 한다.
#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;
int main()
{
vector<string> answer;
vector<string> nameList;
map<string, int> idxList;
int M, N;
cin >> M >> N;
for (int i = 0; i < M; i++)
{
string str;
cin >> str;
nameList.push_back(str);
idxList.insert(make_pair(str, i + 1));
}
for (int i = 0; i < N; i++)
{
string str;
cin >> str;
if (str[0] >= 'A' && str[0] <= 'Z')
answer.push_back(to_string(idxList.find(str)->second));
else
answer.push_back(nameList[stoi(str) - 1]);
}
for (int i = 0; i < N; i++)
cout << answer[i] << '\n';
return 0;
}
nameList와 idxList 두 개를 사용하여 구현하였다.
입력이 들어오면 nameList에는 포켓몬 이름을 저장하고, idxList에는 이름과 번호를 저장해주었다.
추 후 인덱스로 입력이 들어오는 경우는 nameList를 이용해 답을 저장해주었고, 이름으로 들어오는 경우는 find() 를 활용하여 저장하였다.
'CSE > 코딩 문제풀이' 카테고리의 다른 글
[백준 C++] 9095 : 1, 2, 3 더하기 (0) | 2025.02.22 |
---|---|
[백준 C++] 2579 : 계단 오르기 (0) | 2025.02.22 |
[백준 C++] 7576 : 토마토 (0) | 2025.02.21 |
[백준 C++] 1927 : 최소 힙 (0) | 2024.12.16 |
[백준 C++] 11659 : 구간 합 구하기 4 (0) | 2024.12.05 |
@NiffJB :: 개발하는 니프
CSE & GAME 개발 블로그
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 부탁드립니다!