쾌락없는 책임 (공부)/알고리즘 문제풀이
[Algorithm] 백준 1620 나는야 포켓몬 마스터 이다솜 - C++, map
허스크
2022. 4. 15. 16:47
반응형
1620번: 나는야 포켓몬 마스터 이다솜
첫째 줄에는 도감에 수록되어 있는 포켓몬의 개수 N이랑 내가 맞춰야 하는 문제의 개수 M이 주어져. N과 M은 1보다 크거나 같고, 100,000보다 작거나 같은 자연수인데, 자연수가 뭔지는 알지? 모르면
www.acmicpc.net
#include <iostream>
#include <string>
#include <map>
using namespace std;
int n, m;
string pokemons[100001];
map<string, int> pokemon;
int main(){
//init
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// input
cin >> n >> m;
for(int i = 1; i <= n; i++){
string input; cin >> input;
pokemons[i] = input;
pokemon[input] = i;
}
for(int i = 0; i < m; i++){
string problem; cin >> problem;
if(isdigit(problem[0])){
cout << pokemons[stoi(problem)] << '\n';
}
else{
cout << pokemon[problem] << '\n';
}
}
}
C++의 map 연습용으로 풀어본 문제입니다. 일단 이름의 경우 map 로 받으면 되고 번호로 받는건 map 에서 어떻게 처리해야 할지 몰라서 배열로 풀이를 했습니다. 찾아보니 대부분 이 방법을 쓰는거 같고 map 에서 value로 값을 찾는건 비효율적인거 같네요.
반응형