프로그래밍 공부/백준 (C++)

[C++/백준 10816번]

Rocketbabydolls 2023. 8. 14. 22:13

https://www.acmicpc.net/problem/10816

 

10816번: 숫자 카드 2

첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,0

www.acmicpc.net

 

해결 방법

   

   map 의 키값은 중복을 허용하지 않으므로, 가지고 있는 카드를 입력을 받을 때 이전에 추가한 key인지 확인 후 중복되면 해당 숫자의 value 값을 늘려 주는 방식으로 해결했다. map의 키값은 중복이 안된다는 것을 명심하자.

   

 

#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
unordered_map<int, int> m;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int N;

    cin >> N;

    for (int i = 0; i < N; i++)
    {
        int input;
        cin >> input;
        
        if (m.find(input) != m.end())
        {
            m[input]+=1;
        }
        else
        {
            m.insert({ input, 1 });
        }
    }

    int M;

    cin >> M;

    for (int i = 0; i < M; i++)
    {
        int input;
        cin >> input;
        cout << m[input] << " ";
    }

    return 0;
}