50054. A Hash Table

難度:2/5

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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

typedef struct string{
char st[105];
int len;
} string;

typedef struct slot{
struct string* d[10005];
int len;
} slot;

int s, q, n;
string str_arr[10005];
slot hash_table[1005];

int hash(string *now_s){
int ans = 0;
for(int i = 0; i < now_s->len; i++){
if(isalpha(now_s->st[i])) ans += now_s->st[i];
else ans += (now_s->st[i] - '0');
}
return ans % s;
}

void add(slot* t, string* now_s){
t->d[t->len++] = now_s;
}

int check(slot* t, string* now_s){
for(int i = 0; i < t->len; i++){
if(strcmp((t->d[i]->st), now_s->st) == 0){
return true;
}
}
return false;
}

int main(){
scanf("%d %d %d", &s, &n, &q);
for(int i = 0; i < n; i++){
scanf("%s", str_arr[i].st);
str_arr[i].len = strlen(str_arr[i].st);
}

for(int i = 0; i < s; i++){
hash_table[i].len = 0;
}

for(int i = 0; i < n; i++){
int h = hash(&str_arr[i]);
add(&hash_table[h], &str_arr[i]);
}

for(int i = 0; i < q; i++){
string now_s; scanf("%s", now_s.st);
now_s.len = strlen(now_s.st);
int h = hash(&now_s);
if(check(&hash_table[h], &now_s)) printf("%d\n", h);
else printf("%d\n", -1);
}

}


50054. A Hash Table
https://aaronlin1229.github.io/judgegirl_50054/
Author
Akizumi
Posted on
July 17, 2023
Licensed under