본문 바로가기
CS(Computer Science)지식/[C++][코딩 테스트] 자료구조 및 알고리즘

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

by 엔지니어 청년 2024. 1. 31.

알고리즘(코딩 테스트) 공부를 안한지 2달째.. 현업에서 막상 코드 유지보수 업무를 하다보니까 생각보다 코드를 건드리거나 구현할 일이 적은 것 같다. 그래서 알고리즘을 취미로 꾸준히 하려고 한다. 막상 오랜만에 다시 시작하니까 ㅋㅋㅋㅋㅋㅋㅋ 손이 안떨어진다... 코딩의 길 멀고도 험하다..

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

#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;
unordered_map<string, string> p1;
unordered_map<string, string> p2;

int n,m;

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

	cin >> n >> m;
	string s;
	for (int i = 1; i <= n; i++)
	{
		cin >> s;
		p1.insert(make_pair(s, to_string(i)));
		p2.insert(make_pair(to_string(i), s));
	}

	for (int i=0 ; i<m ;i++)
	{
		cin >> s;
		
		cout << p1[s];
		cout << p2[s];
		cout << '\n';
	
	}
	
}

아래는 바킹독님 풀이다.

// http://boj.kr/6c00940e0dab4c4e994d524d38e5582c
#include 
using namespace std;

unordered_map<string, int=""> s2i;
string i2s[100005];

int main(void){
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for(int i = 1; i <= n; i++){
    cin >> i2s[i];
    s2i[i2s[i]] = i;
  }
  while(m--){
    string query;
    cin >> query;
    if(isdigit(query[0])) // isdigit 문자열의 특정 원소가 1~9 숫자인지 판단하는 함수
      cout << i2s[stoi(query)] << '\n'; // stoi string to int 숫자 문자를 숫자로 바꿔주는 함수
    else
      cout << s2i[query] << '\n';
  }
}
</string,>