알고리즘

C++ 알고리즘 - 백준 1764 듣보잡 (unordered_map)

마루설아 2025. 1. 4. 19:31

https://www.acmicpc.net/problem/1764

 

#include <bits/stdc++.h>
#include <unordered_map>
#define endl "\n"

using namespace std;

int main(void) {
	/************** C++ Init **************/
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	/************** C++ Init **************/


	int input1, input2;
	int cnt = 0;
	string str;
	vector<string> v;
	unordered_map<string, int> um;
	cin >> input1 >> input2;

	for (int i = 0; i < input1; i++) {
		cin >> str;
		um.insert({ str, 0 });
	}

	for (int i = 0; i < input2; i++) {
		cin >> str;
		if (um.count(str) != 0) um[str] = 1;
	}

	for (auto em : um) {
		if (em.second == 1) v.push_back(em.first);
	}

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

	cout << v.size() << endl;

	for (int i = 0; i < v.size(); i++) {
		cout << v[i] << endl;
	}
}