최대 1 분 소요

Question

1764Q


Solution

  • set으로 듣도 못한 사람의 명단을 받는다.
  • 보도 못한 사람이 듣도 못한 사람의 명단과 중복되면 vector에 따로 저장한다.
  • 사전순을 위해 vector를 정렬 후 출력하면 된다.

Cord

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

set<string> s; // 듣도 못한 사람 명단
vector<string> v; // 듣보잡 명단

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

	int n, m;
	string name;

	cin >> n >> m;


	// 듣도 못한 사람
	for (int idx = 0; idx < n; idx++)
	{
		cin >> name;
		s.insert(name);
	}

	// 보도 못한 사람
	for (int idx = 0; idx < m; idx++)
	{
		cin >> name;
		if (s.find(name) != s.end())
		{
			v.push_back(name);
		}
	}

	// 사전 순 정렬
	sort(v.begin(), v.end());

	// 결과 출력
	cout << v.size() << '\n';
	for (string str : v)
	{
		cout << str << '\n';
	}

	return 0;
}

Result

1764

set을 이용해보고 map을 이용해 보았다.
보이다시피 map으로 이용하면 set보다 훨씬 느렸다. (코드는 더 간략했는데 말이다..)