백준

[C++] 백준 1620번 나는야 포켓몬 마스터 이다솜

MINU.SHINNNN 2023. 1. 26. 18:02

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

 

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

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

www.acmicpc.net

풀이

1. 두개의 unordered_map 을 사용해서, 

m<이름, 등장하는 번호>

m2<등장하는 번호, 이름> 으로 저장한다.

2. 숫자와 문자열을 구별하기 위해, 입력 받은 후 가장 앞 문자와 비교해서 범위가 1~9라면 m2에서 m.at()을 사용해 이름을 출력하고, 문자열이라면 m[이름] 으로 등장 번호를 출력한다.

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>

using namespace std;

int N, M;
string str;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    // freopen("1620.txt", "r", stdin);
    cin >> N >> M; 
    unordered_map<string, int> m;
    unordered_map<int, string> m2;
    for (int i=0; i<N; i++){
        cin >> str;
        m[str] = i+1;
        m2[i+1]=str;
    }
    for (int i=0; i<M; i++){
        cin >> str;
        if (str[0]-'0' > 0 && str[0]-'0' < 10) {
            int n = stoi(str);
            cout << m2.at(n) << "\n";
        }
        else {
            cout << m[str] << "\n";
        }
    }
    return 0; 
    
}