File tree Expand file tree Collapse file tree 3 files changed +93
-0
lines changed
Expand file tree Collapse file tree 3 files changed +93
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments