Add pset4
This commit is contained in:
parent
a4f5a82f7e
commit
feb2acf4d1
93
semester3/pset4/7-3-航空公司VIP客户查询.c
Normal file
93
semester3/pset4/7-3-航空公司VIP客户查询.c
Normal 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;
|
||||
}
|
Loading…
Reference in a new issue