-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHASHIT.cpp
More file actions
144 lines (113 loc) · 2.3 KB
/
HASHIT.cpp
File metadata and controls
144 lines (113 loc) · 2.3 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
#include <iostream>
#include <string>
#include <vector>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
#define MAX_CHUNK_SIZE 65536
#define IS_WHITESPACE(x) (((x == 13) || (x == 10) || (x == ' ')))
char arr[10000000] = {0};
char *ptr = arr;
char *end_ptr;
int total;
void parse_data()
{
int c, j;
char *ptr = arr;
while((c = fread(ptr, 1, MAX_CHUNK_SIZE, stdin)) > 0) ptr += c;
end_ptr = ptr;
ptr = arr;
}
inline void skip_whitespaces(){while(IS_WHITESPACE(*ptr)) ptr++;}
inline int extract_int()
{
skip_whitespaces();
char *p;
int number = strtol(ptr, &p, 10);
number = ptr == p ? -1 : number;
while(*ptr >= '0' && *ptr <= '9')ptr++;
return number;
}
inline int extract_string(char *buff)
{
skip_whitespaces();
while(*ptr == ' ') ptr++;
char *beginning = buff;
while(*ptr != 13 && *ptr != 10 && *ptr != ' ' && ptr < end_ptr) *buff++ = *ptr++;
*buff = 0;
return buff - beginning;
}
#define MAX_LENGTH 15
char hash_map[101][MAX_LENGTH + 1];
int calculate_hash(char *buff)
{
int hash = 0;
int pos = 1;
while(*buff){
hash += *buff * pos;
buff++;
pos++;
}
return (hash * 19) % 101;
}
void insert_string(char *buff)
{
int hash = calculate_hash(buff);
int j = 0;
for(int k = 0; k < 101; k++){
if(!strcmp(hash_map[k], buff))
return;
}
while(j < 20){
int pos = (hash + j*j + 23*j) % 101;
if(hash_map[pos][0] == 0){
strcpy(hash_map[pos], buff);
return;
} else{
if(strcmp(hash_map[pos], buff) == 0)
return;
}
j++;
}
}
void delete_string(char *buff)
{
int j = 0;
while(j < 101){
if(strcmp(hash_map[j], buff) == 0){
memset(hash_map[j], 0, sizeof(16 * sizeof(char)));
return;
}
j++;
}
}
int main() {
parse_data();
int T = extract_int();
while(T--){
int N = extract_int();
memset(hash_map, 0, sizeof(hash_map));
// 1=null terminator
// 2=ADD: or DEL: both 4 characters
char temp[MAX_LENGTH + 1 + (4)];
while(N--){
extract_string(temp);
bool add_op = (strncmp(temp, "ADD:", 4) == 0);
if(add_op) insert_string(temp + 4);
if(!add_op) delete_string(temp + 4);
}
int count = 0;
for(int j = 0; j < 101; j++){
if(hash_map[j][0])
count++;
}
printf("%d\n", count);
for(int j = 0; j < 101; j++){
if(hash_map[j][0]){
printf("%d:%s\n", j, hash_map[j]);
}
}
}
return 0;
}