CodingTest/백준

백준 1774 - 듣보잡(C++)

빵아찌 2022. 6. 2. 16:18

사전순을 못봐 정렬을 안하여 틀렸지만 쉬운 문제여서 바로 코드를 올립니다.

 

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int iN = 0, iM = 0;
	cin >> iN >> iM;
	map<string, int> map_First;
	vector<string> pAnswer;
	string strTemp;
	pAnswer.reserve(iN > iM ? iN : iM);
	for (int i = 0; i < iN; i++)
	{
		cin >> strTemp;
		map_First.emplace(strTemp, 1);
	}
	for (int i = 0; i < iM; i++)
	{
		cin >> strTemp;
		if (map_First.find(strTemp) != map_First.end())
			pAnswer.emplace_back(strTemp);
	}
	cout << pAnswer.size() << "\n";
	sort(pAnswer.begin(), pAnswer.end());
	for (auto& iter : pAnswer)
		cout << iter << "\n";
	return 0;
}

추후 굳이 map을 안쓰고 vector만 이용하여

binary_search를 사용하여도 된다는 것을 알았습... algorithm에 있다는걸 까먹

 

binary_search : Algorithm 헤더의 함수로써 정렬된 배열을 이진 탐색으로 true와 false로 찾아줌