👉 1620 나는야 포켓몬 마스터 이다솜
안녕? 내 이름은 이다솜. 나의 꿈은 포켓몬 마스터야.😎
Map을 2개 만들어 <포켓몬 번호, 이름> <이름, 포켓몬 번호>로 저장한다.
input의 첫번째 문자가 숫자인지 구별하여 조회한다.
구현은 어렵지 않지만 처음에는 시간초과가 났다.
cout, cin을 사용하면 시간초과가 나므로 printf, scanf를 쓰거나 아래 코드를 추가해야한다.
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
#include <iostream>
#include <map>
using namespace std;
int total, question;
map<int, string> pokemons_index;
map<string, int> pokemons_name;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
string name;
cin >> total >> question;
for (int i=0; i<total; i++) {
cin >> name;
pokemons_index.insert(make_pair(i+1, name));
pokemons_name.insert(make_pair(name, i+1));
}
for (int q=0; q<question; q++) {
cin >> name;
if (atoi(name.c_str()) == 0) {
// str이 문자로 시작한다면 atoi(str)은 0이다.
cout << pokemons_name[name] << "\n";
} else {
cout << pokemons_index[atoi(name.c_str())] << "\n";
}
}
}