백준/집합과 맵

1764번: 듣보잡 [C++]

Koalitsiya 2023. 3. 31. 11:23

문제

 

 

풀이

n + m번 동안 문자열을 받으며 value가 1보다 크면 듣보잡이기 때문에 해당 조건을 만족하는 문자열들을 따로 배열에 담은 후, 배열의 크기를 출력하고, 원소들을 사전순으로 출력하면 된다.

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

int main() {
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL); 
	cout.tie(NULL);

	int n, m;
	string s;

	map<string, int> mp;
	vector<string> result;

	cin >> n >> m;

	for (int i = 0; i < n + m; i++) {
		cin >> s;

		mp[s]++;
		if (mp[s] > 1)
			result.push_back(s);
	}

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

	cout << result.size() << "\n";

	for (int i = 0; i < result.size(); i++)
		cout << result[i] << "\n";

	return 0;
}