1764 - 듣보잡

solution

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;
}

ref

1764번: 듣보잡