SW
[백준 1764] 듣보잡 본문
0. 제목
- 백준 1764 듣보잡
- BOJ 1764 듣보잡
- C++ 1764 듣보잡
1. 문제
https://www.acmicpc.net/problem/1764
2. 풀이
- string자료형을 가지고 있는 vector를 이용한다.
- binarySearch를 이용하여 탐색시간을 줄인다.
- binarySearch로 듣도 못한 사람이 들어있는 vector v를 탐색해서 보도 못한 사람과 같은 이름이 있으면 듣도 보도 못한 사람을 넣는 vector result에 push_back으로 넣어준다.
- vector result를 sort를 사용해 오름차순 정렬을 한다.
- size()를 사용해 개수를 출력하고, 원소들을 출력해준다.
3. 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N, M;
vector<string> v;//듣도 못한 사람
bool binarySearch(int low, int high, string name){
if(low > high)
return false;
else{
int mid = (low + high) / 2;
if(v[mid] == name)
return true;
else if(v[mid] > name)
return binarySearch(low, mid - 1, name);
else
return binarySearch(mid + 1, high, name);
}
}
int main(int argc, const char * argv[]) {
cin >> N >> M;
for(int i = 0; i < N; i++){
string name;
cin >> name;
v.push_back(name);
}
sort(v.begin(), v.end());
vector<string> result;
for(int i = 0; i < M; i++){
string name;
cin >> name;
//듣도 보도 못한 사람
if(binarySearch(0, (int)v.size()-1, name))
result.push_back(name);
}
sort(result.begin(), result.end());
cout << result.size() << '\n';
for(int i = 0; i < result.size(); i++)
cout << result[i] << '\n';
return 0;
}
|
'대학교 > Algorithm' 카테고리의 다른 글
[백준 1325] 효율적인 해킹 (0) | 2020.02.26 |
---|---|
[백준 2606] 바이러스 (0) | 2020.02.26 |
[백준 6581] HTML (0) | 2020.02.24 |
[백준 2002] 추월 (0) | 2020.02.24 |
[백준 1094] 막대기 (0) | 2020.02.23 |
Comments