-
Notifications
You must be signed in to change notification settings - Fork 0
/
HASHIT-2015870-src.c
79 lines (74 loc) · 1.12 KB
/
HASHIT-2015870-src.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *table[101];
char s[1001];
int cnt;
int hash()
{
int ret=0, i;
for(i=4; s[i]; i++)
ret=(ret+s[i]*(i-3))%101;
ret=(19*ret)%101;
return ret;
}
void insert(int f)
{
int idx=hash(), j, i, free=-1;
for(j=0; j<20; j++)
{
i=(idx+j*(23+j))%101;
if(!strcmp(table[i], s+4))
{
if(f)
{
//printf(" %s deleted\n", s+4);
table[i]="";
cnt--;
}
//if(!f)
//printf(" %s duplicate found\n", s+4);
return;
}
if(free==-1 && !strlen(table[i]))
free=i;
}
if(f)
return;
if(free==-1)
{
//printf(" No free location for %s\n", s+4);
return;
}
table[free]=(char *)malloc(strlen(s+4)+1);
strcpy(table[free], s+4);
cnt++;
//printf(" %s inserted at %d\n", s+4, free);
}
int main()
{
int t, i, n;
scanf("%d", &t);
while(t--)
{
//Preprocessing
cnt=0;
for(i=0; i<101; i++)
table[i]="";
//Input
scanf("%d\n", &n);
for(i=0; i<n; i++)
{
gets(s);
if(s[0]=='A')
insert(0);
else
insert(1);
}
printf("%d\n", cnt);
for(i=0; i<101; i++)
if(strlen(table[i]))
printf("%d:%s\n", i, table[i]);
}
return 0;
}