From feb2acf4d17abc35246310ad667d2892d51e906c Mon Sep 17 00:00:00 2001 From: juan Date: Wed, 2 Nov 2022 10:28:10 +0800 Subject: [PATCH] Add pset4 --- semester3/pset4/7-3-航空公司VIP客户查询.c | 93 +++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 semester3/pset4/7-3-航空公司VIP客户查询.c diff --git a/semester3/pset4/7-3-航空公司VIP客户查询.c b/semester3/pset4/7-3-航空公司VIP客户查询.c new file mode 100644 index 0000000..633a72a --- /dev/null +++ b/semester3/pset4/7-3-航空公司VIP客户查询.c @@ -0,0 +1,93 @@ +#include +#include +#include +#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; +}