Skip to content

Commit e8f0166

Browse files
committed
ch04.boj_1764,1920
1 parent 91beff5 commit e8f0166

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Binary file not shown.

Source/ch04/1764.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
int N, M;
8+
9+
vector<string> vec;
10+
11+
bool binaraySearch(string target) {
12+
int left = 0, right = vec.size() - 1;
13+
while(left < right) {
14+
int mid = (left + right) / 2;
15+
if(vec[mid] < target) left = mid + 1;
16+
else right = mid;
17+
}
18+
return (vec[(left + right) / 2] == target);
19+
}
20+
21+
int main() {
22+
ios_base::sync_with_stdio(false);
23+
cin.tie(NULL); cout.tie(NULL);
24+
25+
cin >> N >> M;
26+
27+
string tmp;
28+
for(int i = 0; i < N; i++) {
29+
cin >> tmp;
30+
vec.push_back(tmp);
31+
}
32+
33+
sort(vec.begin(), vec.end());
34+
35+
vector<string> vec1;
36+
for(int i = 0; i < M; i++) {
37+
cin >> tmp;
38+
if(binaraySearch(tmp)) {
39+
vec1.push_back(tmp);
40+
}
41+
}
42+
43+
sort(vec1.begin(), vec1.end());
44+
cout << vec1.size() << "\n";
45+
for(string i : vec1) {
46+
cout << i << "\n";
47+
}
48+
49+
return 0;
50+
}

Source/ch04/1920.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
int N, M;
8+
9+
vector<int> vec;
10+
11+
bool binaraySearch(int num) {
12+
int left = 0, right = vec.size() - 1;
13+
while(left < right) {
14+
int mid = (left + right) / 2;
15+
if(vec[mid] < num) left = mid + 1;
16+
else right = mid;
17+
}
18+
return (vec[(left + right) / 2] == num);
19+
}
20+
21+
int main() {
22+
ios_base::sync_with_stdio(false);
23+
cin.tie(NULL); cout.tie(NULL);
24+
25+
cin >> N;
26+
27+
int tmp;
28+
for(int i = 0; i < N; i++) {
29+
cin >> tmp;
30+
vec.push_back(tmp);
31+
}
32+
33+
sort(vec.begin(), vec.end());
34+
35+
cin >> M;
36+
37+
for(int i = 0; i < M; i++) {
38+
cin >> tmp;
39+
cout << binaraySearch(tmp) << "\n";
40+
}
41+
42+
return 0;
43+
}

0 commit comments

Comments
 (0)