Post

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

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

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

접근

그냥 숫자 검색은 vector, 단어검색은 map으로 저장하면 되겠다 생각했는데 찾아보니
unordered_map(hashmap)이라는 것이 있었다.
hash table을 이용해서 저장하니까 더 빨리 검색될 거 같았다.

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>

using namespace std;
int main(){

  ios::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<string> m1;
  unordered_map<string, int> m2; //unordered_map사용
  for(int i = 1; i <= n; i++){
    string s;
    cin >> s;
    m1.push_back(s);
    m2.insert({s, i});
  }
  while(m--){
    string s;
    cin >> s;
    if(atoi(s.c_str())){ //입력이 수인지 문장인지 판별
      cout << m1[atoi(s.c_str())-1] << "\n";
    }
    else{
      cout << (*m2.find(s)).second << "\n";
    }
  }
  
  
  return 0;
}

배운 점

나름 머리 좀 식히려고 푼 문제인데 얻어간게 좀 있다.
일단 unordered_map 쓰는 것
그리고 atoi로 수인지 문장인지 판별하는 방법을 배웠다.

This post is licensed under CC BY 4.0 by the author.