Notice
Recent Posts
Recent Comments
Link
Little Jay
[C++] 백준 1764번 - 듣보잡 본문
마지막에 조건으로 이름을 사전순으로 출력해야 되는것을 제대로 못보고
왜 틀렸는지 하루종일 보고 있었다.....
일단 맵이나 해쉬로 풀지 않았다.
이거 같은 경우는 그냥 벡터를 계속 정렬해주면서
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;
}
'알고리즘 > BOJ' 카테고리의 다른 글
| [C++] 백준 4889번 - 안정적인 문자 (0) | 2021.08.17 |
|---|---|
| [C++] 백준 7568번 - 덩치 (0) | 2021.08.17 |
| [C++] 백준 1620 - 나는야 포켓몬 마스터 이다솜 (0) | 2021.08.17 |
| [C++] 백준 11659 - 구간 합 구하기 4 (0) | 2021.08.16 |
| [C++] 백준 11399번 - ATM (0) | 2021.08.16 |
Comments