문제
https://www.acmicpc.net/problem/10816
10816번: 숫자 카드 2
첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,
www.acmicpc.net
알고리즘 분류 : 해시를 사용한 집합과 맵
시간복잡도: O(n+m) 이하
난이도: silver 4
해시 맵이 궁금하시다면 다음 글을 참고해주세요.
https://yhoons.tistory.com/32\
hash map - C++ STL
예시 #include #include int main() { std::unordered_map hashmap; //insert hashmap.insert({"key1","value1"}); hashmap.insert({"key2","value2"}); hashmap.insert({"key3","value3"}); //find std::cout first
yhoons.tistory.com
unordered map을 사용하여 풀었습니다.
#include<iostream>
#include<vector>
#include<unordered_map>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int N,M;
int num;
std::unordered_map<int, int> cards;
std::cin >> N;
for(int i=0; i<N; i++)
{
std::cin >> num;
if(cards.find(num) != cards.end())
cards[num]++;
else
cards[num]=1;
}
std::cin >> M;
for(int i=0; i<M; i++)
{
std::cin >>num;
if(cards.find(num) == cards.end())
std::cout << "0";
else
std::cout << cards.find(num)->second;
if(i != M-1)
std::cout << ' ';
}
}
'Baekjoon' 카테고리의 다른 글
[백준]1052번 - 물병 C++ (0) | 2023.08.11 |
---|