-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutualMin_hm.c
129 lines (112 loc) · 2.53 KB
/
mutualMin_hm.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASH_MAP_SIZE 10007
typedef struct HashMapNode
{
unsigned int key;
int count;
struct HashMapNode *next;
} hash_map_node_t;
typedef struct HashMap
{
hash_map_node_t *nodes[HASH_MAP_SIZE];
} hash_map_t;
unsigned int hash(unsigned int key)
{
return key % HASH_MAP_SIZE;
}
hash_map_node_t *hash_map_node_create()
{
hash_map_node_t *node = (hash_map_node_t *)malloc(sizeof(hash_map_node_t));
memset(node, 0, sizeof(hash_map_node_t));
return node;
}
hash_map_t *hash_map_create()
{
hash_map_t *map = (hash_map_t *)malloc(sizeof(hash_map_t));
memset(map, 0, sizeof(hash_map_t));
return map;
}
void hash_map_insert(hash_map_t *map, int key)
{
unsigned int index = hash(key);
hash_map_node_t *newEntry = map->nodes[index];
// Look for existing entry.
while (newEntry != NULL)
{
if (newEntry->key == key)
{
newEntry->count++;
return; // Entry found.
}
newEntry = newEntry->next;
}
// Create new entry.
newEntry = hash_map_node_create();
newEntry->key = key;
newEntry->count = 1;
// Insert new entry to top of chain.
newEntry->next = map->nodes[index];
map->nodes[index] = newEntry;
}
int hash_map_query(hash_map_t *map, int key)
{
unsigned int index = hash(key);
hash_map_node_t *entry = map->nodes[index];
while (entry != NULL)
{
if (entry->key == key)
{
return entry->count;
}
entry = entry->next;
}
// Not found.
return 0;
}
void hash_map_destroy(hash_map_t *map)
{
// Free nodes.
for (int i = 0; i < HASH_MAP_SIZE; ++i)
{
hash_map_node_t *entry = map->nodes[i];
if (entry == NULL)
{
continue;
}
while (entry->next != NULL)
{
hash_map_node_t *prev_entry = entry;
entry = entry->next;
free(prev_entry);
}
free(entry);
}
}
int main(void)
{
int m, n, buffer;
scanf("%d %d", &m, &n);
hash_map_t *map = hash_map_create();
int *arr2 = (int *)malloc(n * sizeof(int));
for (int i = 0; i < m; i++)
{
scanf("%d", &buffer);
hash_map_insert(map, buffer);
}
for (int i = 0; i < n; i++)
{
scanf("%d", &arr2[i]);
}
for (int i = 0; i < n; i++)
{
if (hash_map_query(map, arr2[i]) == 1)
{
printf("%d", arr2[i]);
return 0;
}
}
printf("-1");
return 0;
}