반응형
https://www.acmicpc.net/problem/1620
10815번: 숫자 카드
첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,
www.acmicpc.net
- 문제
포켓몬 이름을 저장한 다음에 번호가 주어지면 해당 번호에 맞는 포켓몬 이름을 출력하는 문제다.
- 해설
map<string,int>랑 map<int,string>을 만들거나
map<string,int>랑 vector<string>을 만들면 된다.
- 풀이
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, M, tempInt;
vector<string> names;
names.reserve(100001);
map<string, int> m;
string temp;
cin >> N >> M;
for (int i = 1; i <= N; i++)
{
cin >> temp;
m[temp] = i;
names.push_back(temp);
}
for (int i = 0; i < M; i++)
{
cin >> temp;
if (isdigit(temp[0]))
{
tempInt = stoi(temp);
cout << names[tempInt - 1] << '\n';
}
else
{
cout << m[temp] << '\n';
}
}
return 0;
}
- 새롭게 알게 된 점
어떻게 풀어도 시간초과가 나서
ios_base::sync_with_stdio(false);
cin.tie(NULL);
도 하고 했는데, 계속 시간초과나서 뭐가 문젠가 했는데
cout << ??? << endl;
저 endl이 시간을 엄청 잡아먹는 다는 것을 배웠다.
<< '\n'으로 대체하도록 하자!!!
반응형
'Algorithm > Baekjoon BOJ' 카테고리의 다른 글
[백준][C++] 1764 : 듣보잡 (0) | 2022.10.21 |
---|---|
[백준][C++] 10816 : 숫자 카드 2 (0) | 2022.10.14 |
[백준][C++] 10815 : 숫자 카드 (0) | 2022.10.13 |
[백준][C++] 14425 : 문자열 집합 (0) | 2022.10.13 |
[백준][C++] 24060번 : 알고리즘 수업 - 병합 정렬 1 (2) | 2022.10.11 |