본문 바로가기
백준

[1764] 듣보잡

by kmyobin 2022. 7. 29.

 

듣지도, 보지도 못하는 명단의 교집합을 구하는 문제이다

듣도 못한 사람을 집합에 insert한 다음, 보도 못한 사람이 집합에 있는지 찾는다

찾으면 새로운 string 배열에 넣고, 듣보잡의 수를 1 증가시킨다

 

모두 탐색하고 난 후 string 배열을 사전순으로 정렬한 다음 출력한다

#include <iostream>
#include <set>
#include <algorithm>
using namespace std;

int N, M, cnt;
set<string> myset;
string Answer[500000];

bool cmp(string a, string b) { // 사전순 정렬
	return a < b;
}

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

	cin >> N >> M;
	for (int i = 0; i < N; i++)
	{
		string input;
		cin >> input;
		myset.insert(input);
	}

	for (int i = 0; i < M; i++)
	{
		string input;
		cin >> input;
		if (myset.find(input) != myset.end()) { // 찾으면
			Answer[cnt] = input;
			cnt++;
		}
		else { // 못찾으면 pass
		}
	}
	sort(Answer, Answer + cnt, cmp); // 사전순 정렬
	printf("%d\n", cnt); // 듣도 && 보도
	for (int i = 0; i < cnt; i++)
	{
		printf("%s\n", Answer[i].c_str());
	}
}

 

'백준' 카테고리의 다른 글

[11478] 서로 다른 부분 문자열의 개수  (0) 2022.07.30
[1269] 대칭 차집합  (0) 2022.07.30
[10816] 숫자 카드 2  (0) 2022.07.29
[1620] 나는야 포켓몬 마스터 이다솜  (0) 2022.07.27
[14425] 문자열 집합  (0) 2022.07.27

댓글