50230. Hash Table

難度:3/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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

// #define max(a,b) ((a)>(b)?(a):(b))
// #define min(a,b) ((a)<(b)?(a):(b))

int f(int k, int s){
return (77 * k + 2222) % s;
}

int main(){
int s, c; scanf("%d %d", &s, &c);
int table[100005][12];
memset(table, -1, sizeof(table));

int val;
while(scanf("%d", &val) != EOF){
int key = f(val, s);

int first_slot = -1, exact_idx = -1;
for(int i = 0; i < c; i++){
if(first_slot == -1 && table[key][i] == -1){
first_slot = i;
}
if(table[key][i] == val){
exact_idx = i;
}
}

if(exact_idx == -1){
table[key][first_slot] = val;
}
else{
printf("%d %d\n", val, key);
table[key][exact_idx] = -1;
}
}
}


50230. Hash Table
https://aaronlin1229.github.io/judgegirl_50230/
Author
Akizumi
Posted on
July 17, 2023
Licensed under