백준 문제풀이/실버4

백준 1764번 - 듣보잡

void_melody 2022. 6. 8. 12:51

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

 

1764번: 듣보잡

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

www.acmicpc.net

중복이 없다 했으므로 set을 활용해봤다.

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

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

	int n, m;
	int count = 0;
	cin >> n >> m;

	set<string> s;

	for (int i = 0; i < n; i++)
	{
		string input;
		cin >> input;
		s.insert(input);
	}
	vector<string> v;
	for (int j = 0; j < m; j++)
	{
		string input;
		cin >> input;
		if (s.find(input) != s.end())
		{
			count++;
			v.push_back(input);
		}
	}

	sort(v.begin(), v.end());
	cout << count << '\n';
	for (int i = 0; i < count; i++)
	{
		cout << v.at(i) << '\n';
	}
	return 0;
}