-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_Lab04_2.c
61 lines (44 loc) · 1.26 KB
/
2_Lab04_2.c
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
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compareStrings(const void* a, const void* b) {
return strcmp(*(const char**)a, *(const char**)b);
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
char** hear = (char**)malloc(sizeof(char*) * n);
char** see = (char**)malloc(sizeof(char*) * m);
for (int i = 0; i < n; i++) {
hear[i] = (char*)malloc(sizeof(char) * 21);
scanf("%s", hear[i]);
}
for (int i = 0; i < m; i++) {
see[i] = (char*)malloc(sizeof(char) * 21);
scanf("%s", see[i]);
}
qsort(hear, n, sizeof(char*), compareStrings);
qsort(see, m, sizeof(char*), compareStrings);
int count = 0;
char*** result = (char***)malloc(sizeof(char**) * (n > m ? n : m)); // ÃÖ´ë n, m Áß Å« ¼ö¸¸Å ÇÒ´ç
for (int i = 0; i < n; i++) {
if (bsearch(&hear[i], see, m, sizeof(char*), compareStrings) != NULL) {
result[count] = &hear[i];
count++;
}
}
printf("%d\n", count);
for (int i = 0; i < count; i++) {
printf("%s\n", *result[i]);
}
for (int i = 0; i < n; i++) {
free(hear[i]);
}
for (int i = 0; i < m; i++) {
free(see[i]);
}
free(hear);
free(see);
free(result);
return 0;
}