Add pset4

This commit is contained in:
juan 2022-11-02 10:28:10 +08:00
parent a4f5a82f7e
commit feb2acf4d1
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7

View file

@ -0,0 +1,93 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100023
#define IDLEN 19
#define P 13
typedef struct hashtable {
int m;
int p;
char **key;
int *answer;
} hashTable;
hashTable *newHash() {
hashTable *h = malloc(sizeof(hashTable));
h->m = SIZE;
h->p = P;
h->key = malloc(sizeof(char *) * SIZE);
for (int i = 0; i < SIZE; i++) {
h->key[i] = malloc(sizeof(char) * IDLEN);
h->key[i][0] = '#';
h->key[i][1] = '\0';
}
h->answer = malloc(sizeof(int) * SIZE);
for (int i = 0; i < SIZE; i++) {
h->answer[i] = 0;
}
return h;
}
int hash(hashTable *h, char *str) {
int m = h->m;
int p = h->p;
int ans = 0;
int p_pow = 1;
for (int i = 0; i < IDLEN - 1; i++) {
if (str[i] == 'x') {
ans = (ans + 11 * p_pow) % m;
} else {
ans = (ans + (str[i] - '0' + 1) * p_pow) % m;
}
p_pow = (p_pow * p) % m;
}
int quad = 1;
while (h->key[ans][0] != '#' && strcmp(h->key[ans], str) != 0) {
// collision
ans = (ans + quad * quad) % m;
quad++;
}
return ans;
}
int max(int l, int r) { return (l > r) ? l : r; }
int main(void) {
int n, k;
scanf("%d%d", &n, &k);
char buf[IDLEN];
int tmp;
hashTable *h = newHash();
int strHash;
for (int i = 0; i < n; i++) {
scanf("%s%d", buf, &tmp);
strHash = hash(h, buf);
strcpy(h->key[strHash], buf);
h->answer[strHash] += max(k, tmp);
}
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", buf);
strHash = hash(h, buf);
if (h->key[strHash][0] == '#') {
printf("No Info\n");
} else {
printf("%d\n", h->answer[strHash]);
}
}
return 0;
}