Written by JS970
on
on
1764 - 듣보잡
- 난이도: 실버 4
- 날짜: 2023년 2월 28일
- 상태: Correct
- 추가 검토 여부: No
- 알고리즘 : set
solution
- set에 듣도 못한 사람들을 추가한다.
- 보도 못한 사람들이 set에 포함되어 있다면 이를 듣도 보도 못한 사람들의 set인 nhs에 추가한다.
- nhs의 원소들을 vector컨테이너 ans에 저장한다. ans벡터를 sort한 후 조건에 맞게 출력했다.
code
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, M;
cin >> N >> M;
set<string> nh;
set<string> nhs;
string name;
for(int i = 0; i < N; i++)
{
cin >> name;
nh.insert(name);
}
for(int i = 0; i < M; i++)
{
cin >> name;
if(nh.find(name) != nh.end())
nhs.insert(name);
}
vector<string> ans;
set<string>::iterator iter;
for(iter = nhs.begin(); iter != nhs.end(); iter++)
ans.push_back(*iter);
sort(ans.begin(), ans.end());
cout << ans.size() << endl;
for(int i = 0; i < ans.size(); i++)
cout << ans[i] << '\n';
return 0;
}