forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashit.cpp
64 lines (64 loc) · 996 Bytes
/
hashit.cpp
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
// 2008-07-14
#define CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstring>
char hashtable[101][16];
int hash(char* s)
{
int h=0,i;
for (i=0; s[i]; i++)
h+=s[i]*(i+1);
return (19*h)%101;
}
void add(char* s)
{
int i;
for (i=0; i<101; i++)
if (!strcmp(s,hashtable[i]))
return;
int h=hash(s);
for (i=0; i<20; i++)
if (!hashtable[(h+i*i+23*i)%101][0])
{
strcpy(hashtable[(h+i*i+23*i)%101],s);
return;
}
}
void del(char* s)
{
int i;
for (i=0; i<101; i++)
if (!strcmp(s,hashtable[i]))
{
hashtable[i][0]=0;
return;
}
}
int main()
{
int t,i,j,n;
char st[20];
scanf("%d",&t);
for (i=0; i<t; i++)
{
for (j=0; j<101; j++)
hashtable[j][0]=0;
scanf("%d",&n);
for (j=0; j<n; j++)
{
scanf("%s",st);
if (st[0]=='A')
add(st+4);
else
del(st+4);
}
int c=0;
for (j=0; j<101; j++)
if (hashtable[j][0]) c++;
printf("%d\n",c);
for (j=0; j<101; j++)
if (hashtable[j][0])
printf("%d:%s\n",j,hashtable[j]);
}
return 0;
}