▶실버4
풀이
문자열과 숫자를 구분하여 탐색해야하기 때문에 생각보다는 까다로운 문제였음!
map을 이용하여 <이름, 번호> 인 map 하나, <번호, 이름> 인 map 하나를 생성하여 번호를 찾을 땐 전자, 이름을 찾을 땐 후자를 이용한다.
이때, m번의 입력을 받는 동안 자료형을 구분할 수 없으므로 string 형태로 일단 받고, 그 문자열의 첫 자가 문자인지 숫자인지 구분 후 진행한다.
※ 참고
문자랑 숫자를 구분할 때, isalpha 함수를 사용했는데 런타임에러가 떴다. 쓰지말기!
코드
#include <iostream>
#include <cmath>
#include <cctype>
#include <cstring>
#include <string>
#include <typeinfo>
#include <vector>
#include <stack>
#include <queue>
#include <map>
using namespace std;
map<string, int> number;
map <int, string> name;
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
string str;
cin >> str;
number.insert({ str, i + 1 });
name.insert({ i + 1, str });
}
for (int i = 0; i < m; i++) {
string poket;
cin >> poket;
if (65 <= poket[0] && 90 >= poket[0]) {
cout << to_string(number[poket]) << "\n";
}
else {
cout << name[stoi(poket)] << "\n";
}
}
}
'Algorithm > Baekjoon' 카테고리의 다른 글
3190_뱀 (0) | 2023.02.10 |
---|---|
1874_스택 수열 (0) | 2023.02.08 |
11279_최대 힙 (0) | 2023.02.03 |
10799_쇠막대기 (0) | 2023.02.02 |
17478_재귀함수가 뭔가요? (0) | 2023.01.22 |