[BOJ 1620] 나는야 포켓몬 마스터 이다솜

xeonu·2022년 7월 1일
0

코딩테스트

목록 보기
2/7
post-thumbnail

문제링크

소스코드

#include <string>
#include <iostream>
#include <map>

using namespace std;

int main() {
    ios_base::sync_with_stdio();
    cin.tie(NULL);
    cout.tie(NULL);
    
    int m, n;
    string str;
    map<int, string> dict1;
    map<string, int> dict2;

    cin >> m >> n;
    for (int i = 1; i <= m; i++) {
        cin>> str;
        dict1[i] = str;
        dict2[str] = i;
    }

    while(n--){
        cin>>str;

        //입력값이 문자라면
        if(atoi(str.c_str())==0){
            cout<<dict2[str]<<'\n';
        }
        //입력값이 숫자라면
        else{
            cout<<dict1[atoi(str.c_str())]<<'\n';
        }
    }
}

map을 <key, value>를 저장하는 자료구조로 레드블랙트리로 구현되어있다. string을 입력받고 이것이 숫자인지 영어단어인지 확인하기 위해서 atoi를 이용하였고 이때 atoi는 숫자가 아닌 다른 문자를 입력 받으면 0을 return한다. 또한 일반적인 string을 파라미터로 받을 순 없고 char* 형태로 변환을 해줘야하므로 c_str()을 이용한다.


해당 코드는 비효율적인 풀이로 map과 atoi c_str 사용에 의의를 두었다.

profile
백엔드 개발자가 되기위한 여정

0개의 댓글