본문 바로가기

Algorithm/Baekjoon BOJ

[백준][C++] 1764 : 듣보잡

반응형

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

 

1764번: 듣보잡

첫째 줄에 듣도 못한 사람의 수 N, 보도 못한 사람의 수 M이 주어진다. 이어서 둘째 줄부터 N개의 줄에 걸쳐 듣도 못한 사람의 이름과, N+2째 줄부터 보도 못한 사람의 이름이 순서대로 주어진다.

www.acmicpc.net

 

 

 


- 문제

 

  이름이 포함된 array가 두개 주어지면 둘 모두에 있는 이름을 출력한다.

 

 


- 해설

 

  map<string,int>를 만들어서 두 개의 이름 집합에 대해서 ++해주면 2 이상인 이름이 듣도보도 못한 이름이 된다.

 

 


- 풀이

#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);

int main()
{
    fastio;
    int N, M;
    string temp;
    map<string, int> m;
    vector<string> s;
    s.reserve(N);
    cin >> N >> M;
    for (int i = 0; i < N; i++)
    {
        cin >> temp;
        m[temp]++;
    }
    for (int i = 0; i < M; i++)
    {
        cin >> temp;
        m[temp]++;
    }
    for (auto mm : m)
    {
        if (mm.second > 1)
            s.push_back(mm.first);
    }
    cout << s.size() << '\n';
    for (auto ss : s)
    {
        cout << ss << '\n';
    }
    return 0;
}

- 새롭게 알게 된 점

 

#define fastio 이런 방식으로 사람들이 많이 만들길래, 한 번 따라해봤다.

반응형