백준/1764/자료 구조/듣보잡

유기태·2024년 1월 19일

백준/1764/자료 구조/듣보잡

문제 해석

처음에는 듣지도 못한사람 다음은 보지도 못한 사람을 입력으로 주는데 이때 이 두 리스트에서 중복이 되는 사람들을 찾는 문제입니다.

문제 풀이

간단하게 map 자료구조(정렬)을 이용해 문제를 해결했습니다.

풀이

첫번째 풀이

#include<iostream>
#include<map>
using namespace std;

map<string, bool> _map;

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	int N, M = 0;
	cin >> N >> M;
	string s = "";
	for (int i = 0;i < N;i++)
	{
		cin >> s;
		_map.insert(make_pair(s, false));
	}

	map<string, bool>::iterator iter;


	int _count = 0;
	for (int i = 0;i < M;i++)
	{
		cin >> s;
		iter = _map.find(s);

		if (iter != _map.end())
		{
			iter->second = true;
			_count++;
		}
	}

	cout << _count << '\n';
	for (iter = _map.begin();iter != _map.end();iter++)
	{
		if (iter->second == true)
		{
			cout << iter->first << '\n';
		}
	}
	
	return 0;
}
profile
게임프로그래머 지망!

0개의 댓글