본문 바로가기

알고리즘

Boj 1620 c++ 나는야 포켓몬 마스터 이다솜

오랜만에 푸는 집합과 맵 문제. 일단 문제 부터가 재미있다. 

풀이한 방법은 양방향 unordered_map이다. 예상치 못하게 고전한 부분은 입력받은 값이 문자열인지 숫자인지 구분하는 부분.

하지만 cctype의 내장함수 isinteger를 사용하여 해결하였다.

#include<iostream>
#include<cstring>
#include<cctype>
#include<unordered_map>

using namespace std;

int main(){

  ios_base :: sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int N, M;

  unordered_map<string, int> f;
  unordered_map<int, string> b;
  string str;

  cin>>N>>M;

  for(int i=1; i<=N; i++){

    cin>>str;
    f[str]=i;
    b[i]=str;
  }
  for(int i=0; i<M; i++){
    cin>>str;
    if(isdigit(str[0])){
     cout<<b[stoi(str)]<<"\n";

    }
    else{
        cout<<f[str]<<"\n";
    }

  }



}

'알고리즘' 카테고리의 다른 글

Boj 2468 c++ 안전 영역  (1) 2025.06.04
Boj 1012 c++ 유기농 배추(feat: 연결 요소의 개수)  (0) 2025.06.03
Boj 2036 c++ 수열의 점수  (0) 2025.06.03
Boj 30960 c++ 눈물의 조별과제  (0) 2025.06.03
Boj 21758 c++ 꿀따기  (0) 2025.06.03