-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
162 lines (132 loc) · 3.69 KB
/
code
File metadata and controls
162 lines (132 loc) · 3.69 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
using namespace std;
typedef struct Entry {
string key = "";
bool occupied = false; // Indicate whether the slot is occupied
}Entry;
typedef struct Dictionary {
int size;
int cnt;
Entry* hash;
};
Dictionary* create_dict(int size) {
Dictionary* d = new Dictionary;
d->size = size;
d->cnt = 0;
d->hash = new Entry[size];
return d;
}
int hash_function(string key, int size, int n) {
int sum = 0;
for (int t = 0; t < key.length(); t++) {
sum += key[t] * (t + 1);
}
sum *= 19;
int pos = abs(sum % 101);
return pos;
}
int colision(string key, int number){
return (hash_function(key, 101, 101) + (number * number) + (23 * number)) % 101;
}
int find(Dictionary* d, string key) {
int pos = hash_function(key, d->size, d->cnt);
int newpos = pos;
if (d->hash[pos].key == key && d->hash[pos].occupied) {
return pos;
}
int g = 1;
while (g < 20) {
newpos = colision(key, g);
if (d->hash[newpos].key == key && d->hash[newpos].occupied) {
return newpos;
}
g++;
}
return -1;
}
void insert(Dictionary* d, string key) {
int index = find(d, key);
if (index != -1 ) {
return; // Element not found
}
int pos = hash_function(key, d->size, d->cnt);
int newpos = pos; // Initialize newpos with pos
if (d->hash[pos].key == "" and !d->hash[pos].occupied and 101 > d->cnt){
d->hash[pos].key = key;
d->hash[pos].occupied = true;
d->cnt++;
return;
}
else if (d->hash[newpos].occupied and 101 > d->cnt and d->hash[pos].key != ""){
int g = 1;
while (g < 20) {
newpos = colision(key, g);
if (d->hash[newpos].key == "" and !d->hash[newpos].occupied) {
d->hash[newpos].key = key;
d->hash[newpos].occupied = true;
d->cnt++;
return;
}
g++;
}
}
}
void remove(Dictionary* d, string key) {
int index = find(d, key);
if (index == -1) {
return; // Element not found
}
int pos = hash_function(key, d->size, d->cnt);
int newpos = pos; // Initialize newpos with pos
if (d->hash[newpos].key == key and d->hash[newpos].occupied) {
d->hash[newpos].key = "";
d->hash[newpos].occupied = false;
d->cnt--;
return;
}
int g = 1;
while (g < 20) {
newpos = colision(key, g);
if (d->hash[newpos].key == key) {
d->hash[newpos].key = "";
d->hash[newpos].occupied = false;
d->cnt--;
return;
}
g++;
}
}
int main() {
int casos, iteracoes;
string nome;
cin >> casos;
for (int w = 0; w < casos; w++) {
int i = 0;
cin >> iteracoes;
Dictionary* table = create_dict(101);
while (iteracoes > i) {
string comando;
cin >> comando;
string command = "";
string object = "";
command = command + comando[0];
for (int k=4; k < comando.length(); k++){
object = object + comando[k];
}
if (comando[0] == 'A') {
insert(table, object);
} else if (comando[0] == 'D') {
remove(table, object);
}
i++;
}
cout << table->cnt << endl;
for (int y = 0; y < 101; y++) {
if (table->hash[y].key != "" ) {
cout << y << ":" << table->hash[y].key << endl;
}
}
delete table;
}
return 0;
}