🔗문제 풀러가기
단계별로 풀어보기 단계 14의 4번째 문제이다.
Map 컨네이너를 이용해 문제를 해결하였다.
#include <iostream>
#include <map>
#include <string>
using namespace std;
string arr[100001];
int main()
{
cin.tie(NULL); //안 하면 시간초과
ios_base::sync_with_stdio(false); //안 하면 시간초과
int n, m;
cin >> n >> m;
map<string, int> _map;
for (int i = 1; i <= n; i++)
{
string input;
cin >> input;
_map.insert(make_pair(input, i));
arr[i] = input;
}
for (int i = 0; i < m; i++)
{
string input;
cin >> input;
if (_map.find(input) != _map.end())
{
cout << _map.find(input)->second << "\n";
}
else
{
int index = stoi(input);
cout << arr[index] << "\n";
}
}
}