[백준] 1764번 듣보잡 / C++
#문제
#풀이
#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: