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

CodingTest/백준|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로 찾아줌

 

'CodingTest > 백준' 카테고리의 다른 글

백준 18870 - 좌표 압축(C++)  (0) 2022.06.02
백준 17219 - 비밀번호 찾기(C++)  (0) 2022.06.02
백준 18111 - 마인크래프트(C++)  (0) 2022.06.02
백준 2108 - 통계학(C++)  (0) 2022.05.31
백준 2805 - 나무 자르기(C++)  (0) 2022.05.31

댓글()