백준 1764 듣보잡 / C++

이유참치·2025년 7월 31일

백준

목록 보기
27/249

문제 : 1764

풀이 point

map을 활용하여 풀 수 있다. 듣도 못한 사람과 보도 못한 사람의 중복을 구하면 된다.

풀이 방법

듣도 못한 사람을 만났을 때 +1 보도 못한 사람을 만났을 떄 +1을 해준다.
map을 순회 하면서 2이상인 사람을 만난다면 듣도 보도 못한 사람이다.

코드

//백준 1764, 듣보잡

#include <iostream>
#include <map>

std::map<std::string, int> map;

int main (){

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    int N, M;
    std::string s;
    std::cin >> N >> M;

    for(int i{0}; i<N; ++i){
        std::cin >> s;
        map[s] = 1;
    }

    for(int i{0}; i<M; ++i){
        std::cin >> s;
        if(map.find(s) != map.end()) ++map[s];
    }
    int ans{0};
    for(auto pair : map){
        if(pair.second > 1){
            ++ans;
        }
    }

    std::cout << ans << '\n';

    for(auto pair : map){
        if(pair.second > 1){
            std::cout << pair.first << '\n';
        }
    }

    return 0;
}

2025-01-26T01:44:50.496Z

profile
임아리 - 대학생

0개의 댓글