본문 바로가기

Algorithm/BAEKJOON

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

728x90
반응형

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

 

1620번: 나는야 포켓몬 마스터 이다솜

첫째 줄에는 도감에 수록되어 있는 포켓몬의 개수 N이랑 내가 맞춰야 하는 문제의 개수 M이 주어져. N과 M은 1보다 크거나 같고, 100,000보다 작거나 같은 자연수인데, 자연수가 뭔지는 알지? 모르면

www.acmicpc.net

 

#include <iostream>
#include <map>
#include <string>
using namespace std;
 
int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
 
	int n, m;
	cin >> n >> m;
 
	map<int, string> m1;
	map<string, int> m2;
	string s;
	for(int i = 1; i <= n; i++){
		cin >> s;
		m1.insert(make_pair(i, s));
		m2.insert(make_pair(s, i));
	}
 
	for(int i = 0; i < m; i++){
		cin >> s;
		if('0' <= s[0] && s[0] <= '9'){
			int idx = stoi(s);
			cout << m1[idx] << '\n';
		} else{
			cout << m2[s] << '\n';
		}
	}
	return 0;
}

map 컨테이너는 [ ]로 key 값을 이용해 value 값을 찾을 수 있다. key값이 string인 map과 int인 map 두 개를 만들어서 입력되는 문제가 string일 경우와 int인 경우 각각 다른 map 컨테이너를 검색하도록 구현하였다.

728x90
반응형

'Algorithm > BAEKJOON' 카테고리의 다른 글

[BOJ] 1316번 그룹 단어 체커 (C++)  (0) 2022.01.08
[BOJ] 11723번 집합 (C++)  (0) 2022.01.06
[BOJ] 1753번 최단경로 (C++)  (0) 2022.01.04
[BOJ] 1764번 듣보잡 (C++)  (0) 2022.01.03
[BOJ] 2075번 N번째 큰 수 (C++)  (0) 2022.01.02