336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
문제 : https://www.acmicpc.net/problem/1620
출력을 위한 입력부분에서 숫자로 들어오는지 또는 스트링으로 들어오는지 2가지 경우가 있다.
2가지 경우를 위해 도감도 쪼개주었다.
사실 메모리 초과가 날까봐 살짝 겁은 났는데 돌아가서 기분이 좋다. ^^
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int regi, qu;
cin >> regi >> qu;
map<string, int> dogam_for_str;
vector<string> dogam_for_index(regi+1);
for(int i = 1; i <= regi; i++){
string str;
cin >> str;
dogam_for_index[i] = str;
dogam_for_str[str] = i;
}
for(int i = 1; i <= qu; i++){
string temp;
cin >> temp;
// 숫자인 경우 바로 인덱스로 접근
if(atoi(temp.c_str()) != 0){
cout << dogam_for_index[atoi(temp.c_str())] << '\n';
}else{
// 문자인 경우
cout << dogam_for_str[temp] << '\n';
}
}
return 0;
}
atoi인 경우 인자가 숫자가 아닌 경우 0을 리턴한다.
해당 문제의 경우 도감의 인덱스가 1부터 시작하기 때문에 해당 함수를 사용하여 풀 수 있었다.
'PS > 백준 문제풀이' 카테고리의 다른 글
[백준 1715] 카드 정렬하기 (0) | 2019.08.29 |
---|---|
[백준 2503] 숫자 야구 (0) | 2019.08.25 |
[백준 1269] 대칭 차집합 (0) | 2019.08.21 |
[백준 1068] 트리 (0) | 2019.08.17 |
[백준 5397] 키로거 (0) | 2019.08.09 |