Little Jay

[C++] 백준 1764번 - 듣보잡 본문

알고리즘/BOJ

[C++] 백준 1764번 - 듣보잡

Jay, Lee 2021. 8. 17. 15:42

마지막에 조건으로 이름을 사전순으로 출력해야 되는것을 제대로 못보고

왜 틀렸는지 하루종일 보고 있었다.....

일단 맵이나 해쉬로 풀지 않았다.

이거 같은 경우는 그냥 벡터를 계속 정렬해주면서 

binary_search로 빠르게 이름을 찾으면 되는 문제인 것 같다.

 

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

int main() {

	ios::sync_with_stdio(false);
	cin.tie(0);

	int n, m;
	cin >> n >> m;

	vector<string> v(n);
	v.resize(n);
	string s;

	for (int i = 0; i < n; i++) {
		cin >> v[i];
	}

	sort(v.begin(), v.end());
	vector<string> temp;

	for (int i = 0; i < m; i++) {
		cin >> s;
		if (binary_search(v.begin(), v.end(), s))
			temp.push_back(s);
	}

	cout << temp.size() << "\n";
	sort(temp.begin(), temp.end());
	for (int i = 0; i < temp.size(); i++) {
		cout << temp[i] << "\n";
	}

	return 0;
}

 

Comments