Algorithm/BOJ

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

kim-hasa 2022. 3. 1. 16:42

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

 

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

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

www.acmicpc.net

문제가 재미있었다. 근데 너무 길어서 이해하기는 좀 힘듬 ...

 

포켓몬을 도감에 입력하고 출력하는 문제였다.

 

처음엔 그냥 벡터를 사용했는데 , 시간 초과가 나서 map을 사용했다.

 

string 배열과 map에 데이터를 입력해놓고 , 찾아서 출력하면 완성이다.

 

다만 , 입력이 워낙 많기 때문에 벡터보다는 map을 사용해야 한다.

 

#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n , m;
    cin >> n >> m;
    
    string arr[100001];
    map<string , int> maps;
    
    for(int i=1; i<=n; i++){
        string s;
        cin >> s;
        
        arr[i] = s;
        maps.insert(make_pair(s , i));
    }
    
    for(int i=0; i<m; i++){
        string str;
        cin >> str;
        
        if(isdigit(str[0]) == true){
            cout << arr[stoi(str)] << '\n';
        }
        else {
            auto it = maps.find(str);
            cout << it->second << '\n';
        }
    }
}