자동으로 오름차순으로 정렬시켜주는 map 자료구조를 이용하자.
insert를 할때 { str, i } 쌍으로 넣어주고 있는데, 중복되는 key값이 없어서 insert결과가 true인 경우 리턴값으로 pair<iterator, bool>을 넘겨주는데 first가 insert된 원소의 iterator이고 second가 true로 반환될 것이다.
그래서 second == false인 경우 이미 이전에 같은 이름을 넣었다는 뜻이므로 듣보잡이 되어 따로 벡터 배열에 저장이 된다.
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m;
cin >> n >> m;
map<string, int> strMap;
vector<string> v;
string str;
for (int i = 0; i < n + m; i++)
{
cin >> str;
if (strMap.insert({ str, i }).second == false)
v.push_back(str);
}
sort(v.begin(), v.end());
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++)
cout << v[i] << endl;
return 0;
}