문제
아래 링크 참고
풀이
<이름, 번호>와 <번호, 이름> 구조의 맵을 따로 만들어서 이름이 들어오면 번호, 번호가 들어오면 이름을 출력하도록 하였다.
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
int cnt = 1;
map<string, int> pokemonNum;
map<int, string> pokemonName;
cin >> n >> m;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
pokemonNum[s] = cnt + i;
pokemonName[cnt + i] = s;
}
for (int i = 0; i < m; i++) {
string s;
cin >> s;
if (s[0] >= 'A' && s[0] <= 'Z')
cout << pokemonNum[s] << "\n";
else
cout << pokemonName[stoi(s)] << "\n";
}
return 0;
}
'백준 > 집합과 맵' 카테고리의 다른 글
1764번: 듣보잡 [C++] (0) | 2023.03.31 |
---|---|
10816번: 숫자 카드 2 [C++] (0) | 2023.03.31 |
14425번: 문자열 집합 [C++] (0) | 2023.03.31 |
10815번: 숫자 카드 [C++] (0) | 2023.03.31 |