[백준] 1764번 듣보잡 / C++

#문제

백준 1764번 듣보잡

#풀이

#include <algorithm>
#include <iostream>
#include <unordered_set>
#include <string>
#include <vector>

using namespace std;

unordered_set<string> nameList;
vector<string> dbj;

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

	int n, m, cnt(0);
	cin >> n >> m;

	while (n--)
	{
		string name;
		cin >> name;

		nameList.insert(name);
	}

	while (m--)
	{
		string name;
		cin >> name;

		if (nameList.find(name) != nameList.end())
		{
			dbj.push_back(name);
			cnt++;
		}
	}

	sort(dbj.begin(), dbj.end());

	cout << cnt << '\n';

	for (int i = 0; i < dbj.size(); ++i)
	{
		cout << dbj[i] << '\n';
	}

	return 0;
}

#정리

n개의 이름을 입력받은 후 m개의 이름을 입력받는다. 두 개의 명단에서 겹치는 이름의 수를 출력하고, 해당 이름들을 사전순으로 출력한다. 해시셋을 이용해 빠르게 찾아 벡터에 넣은 뒤, 사전순으로 sort하여 출력.




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • [백준] 14425번 문자열 집합 / C++
  • [백준] 11656번 접미사 배열 / C++
  • [백준] 1620번 나는야 포켓몬 마스터 이다솜 / C++
  • [백준] 10814번 나이순 정렬 / C++
  • [백준] 1065번 한수 / C++