Add pset4
This commit is contained in:
parent
e23ed8daa6
commit
a4f5a82f7e
|
@ -111,6 +111,7 @@ list *adjust(list *li) {
|
|||
j++;
|
||||
k++;
|
||||
} else if (li->arr[i]->degree == li->arr[j]->degree) {
|
||||
// remember, no need to do anything else.
|
||||
li->arr[i] = merge(li->arr[i], li->arr[j]);
|
||||
erase(li, j);
|
||||
}
|
||||
|
|
|
@ -146,7 +146,7 @@ list *insert(list *l, int key) {
|
|||
return insertTree(l, temp);
|
||||
}
|
||||
|
||||
void traverseLevel(Node* h, int level) {
|
||||
void traverseLevel(Node *h, int level) {
|
||||
if (h) {
|
||||
traverseLevel(h->sibling, level);
|
||||
if (level == 1) {
|
||||
|
@ -159,7 +159,7 @@ void traverseLevel(Node* h, int level) {
|
|||
|
||||
void printTree(Node *h) {
|
||||
int i = 0;
|
||||
for (Node* ptr = h; ptr != NULL; ptr = ptr->child) {
|
||||
for (Node *ptr = h; ptr != NULL; ptr = ptr->child) {
|
||||
i++;
|
||||
traverseLevel(h, i);
|
||||
}
|
||||
|
@ -182,7 +182,7 @@ void printHeap(list *l, int k) {
|
|||
int getDigits(int i) {
|
||||
int count = 0;
|
||||
while (i) {
|
||||
count ++;
|
||||
count++;
|
||||
i /= 10;
|
||||
}
|
||||
|
||||
|
|
146
semester3/pset4/7-1-霍夫曼解码.c
Normal file
146
semester3/pset4/7-1-霍夫曼解码.c
Normal file
|
@ -0,0 +1,146 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define MAX 10000
|
||||
|
||||
typedef struct treeNode {
|
||||
char val;
|
||||
int freq;
|
||||
struct treeNode *left, *right;
|
||||
} treeNode;
|
||||
|
||||
typedef struct heap {
|
||||
int size;
|
||||
treeNode **arr;
|
||||
} heap;
|
||||
|
||||
heap *newHeap() {
|
||||
heap *h = malloc(sizeof(heap));
|
||||
h->size = 0;
|
||||
h->arr = malloc(sizeof(treeNode *) * MAX);
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
int parent(int i) { return (i - 1) / 2; }
|
||||
|
||||
int left(int i) { return i * 2 + 1; }
|
||||
|
||||
int right(int i) { return i * 2 + 2; }
|
||||
|
||||
void swapNode(treeNode **l, treeNode **r) {
|
||||
treeNode *tmp = *l;
|
||||
*l = *r;
|
||||
*r = tmp;
|
||||
}
|
||||
|
||||
void insert(heap *h, treeNode *val) {
|
||||
int i = h->size++;
|
||||
|
||||
h->arr[i] = val;
|
||||
|
||||
while (h->arr[i]->freq < h->arr[parent(i)]->freq) {
|
||||
swapNode(&h->arr[i], &h->arr[parent(i)]);
|
||||
i = parent(i);
|
||||
}
|
||||
}
|
||||
|
||||
void heapify(heap *h, int i) {
|
||||
// from up to down.
|
||||
int smallest = i;
|
||||
int l = left(i), r = right(i);
|
||||
// Check for OOB!!!!!!
|
||||
if (l < h->size && h->arr[smallest]->freq > h->arr[l]->freq) {
|
||||
smallest = l;
|
||||
}
|
||||
if (r < h->size && h->arr[smallest]->freq > h->arr[r]->freq) {
|
||||
smallest = r;
|
||||
}
|
||||
|
||||
if (smallest != i) {
|
||||
swapNode(&h->arr[i], &h->arr[smallest]);
|
||||
heapify(h, smallest);
|
||||
}
|
||||
}
|
||||
|
||||
treeNode *extractMin(heap *h) {
|
||||
treeNode *res = h->arr[0];
|
||||
// printf("Poped: %c %d\n", res->val, res->freq);
|
||||
h->arr[0] = h->arr[h->size - 1];
|
||||
h->size--;
|
||||
|
||||
heapify(h, 0);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
treeNode *newNode(char val, int freq) {
|
||||
treeNode *n = malloc(sizeof(treeNode));
|
||||
|
||||
n->freq = freq;
|
||||
n->val = val;
|
||||
n->left = n->right = NULL;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
treeNode *buildHuffmanTree(heap *h) {
|
||||
while (h->size != 1) {
|
||||
treeNode *l = extractMin(h);
|
||||
treeNode *r = extractMin(h);
|
||||
|
||||
treeNode *new = newNode('#', l->freq + r->freq);
|
||||
new->left = l;
|
||||
new->right = r;
|
||||
|
||||
insert(h, new);
|
||||
}
|
||||
|
||||
return extractMin(h);
|
||||
}
|
||||
|
||||
int isLeaf(treeNode *tr) { return (!tr->left) && (!tr->right); }
|
||||
|
||||
void printCode(treeNode *tr, char *input) {
|
||||
treeNode *ptr;
|
||||
int top = 0;
|
||||
while (input[top] != '\n' && input[top] != '\0') {
|
||||
ptr = tr;
|
||||
while (!isLeaf(ptr)) {
|
||||
if (input[top] == '0') {
|
||||
// printf("%c", input[top]);
|
||||
ptr = ptr->left;
|
||||
top++;
|
||||
} else if (input[top] == '1') {
|
||||
// printf("%c", input[top]);
|
||||
ptr = ptr->right;
|
||||
top++;
|
||||
}
|
||||
}
|
||||
|
||||
// printf(":%c\n", ptr->val);
|
||||
printf("%c", ptr->val);
|
||||
}
|
||||
}
|
||||
|
||||
void printTree(treeNode *tr) {}
|
||||
|
||||
int main(void) {
|
||||
heap *h = newHeap();
|
||||
insert(h, newNode('a', 7));
|
||||
insert(h, newNode('b', 19));
|
||||
insert(h, newNode('c', 2));
|
||||
insert(h, newNode('d', 6));
|
||||
insert(h, newNode('e', 32));
|
||||
insert(h, newNode('f', 3));
|
||||
insert(h, newNode('g', 21));
|
||||
insert(h, newNode('h', 10));
|
||||
|
||||
treeNode *huffmanTree = buildHuffmanTree(h);
|
||||
|
||||
char input[MAX];
|
||||
fgets(input, MAX, stdin);
|
||||
|
||||
printCode(huffmanTree, input);
|
||||
|
||||
return 0;
|
||||
}
|
27
semester3/pset4/7-2-How-to-Place-the-Mail.c
Normal file
27
semester3/pset4/7-2-How-to-Place-the-Mail.c
Normal file
|
@ -0,0 +1,27 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void insert(long long *box, long long val, int i) {
|
||||
int loc = (val + i * i) % 10;
|
||||
if (loc < 10 && box[loc] == 0) {
|
||||
box[loc] = val;
|
||||
} else {
|
||||
insert(box, val, i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
long long box[10] = {};
|
||||
|
||||
long long phone;
|
||||
while (scanf("%lld,", &phone) != EOF) {
|
||||
insert(box, phone, 0);
|
||||
}
|
||||
|
||||
for (int i = 0, size = 10; i < size - 1; i++) {
|
||||
printf("%lld,", box[i]);
|
||||
}
|
||||
printf("%lld", box[9]);
|
||||
|
||||
return 0;
|
||||
}
|
36
semester3/pset4/7-3-航空公司VIP客户查询.cpp
Normal file
36
semester3/pset4/7-3-航空公司VIP客户查询.cpp
Normal file
|
@ -0,0 +1,36 @@
|
|||
#include <iostream>
|
||||
#include <unordered_map>
|
||||
#include <cstdio>
|
||||
#pragma GCC optimize("Ofast")
|
||||
using namespace std;
|
||||
|
||||
int main(void) {
|
||||
int size, minMile;
|
||||
unordered_map<string, int> data;
|
||||
scanf("%d%d", &size, &minMile);
|
||||
// cout << size << ' ' << minMile << '\n';
|
||||
|
||||
char id_char[18];
|
||||
string id;
|
||||
int tmp;
|
||||
for (int i = 0; i < size; i++) {
|
||||
scanf("%s%d", id_char, &tmp);
|
||||
id = id_char;
|
||||
data[id] += max(tmp, minMile);
|
||||
}
|
||||
|
||||
int queries;
|
||||
scanf("%d", &queries);
|
||||
|
||||
for (int i = 0; i < queries; i++) {
|
||||
scanf("%s", id_char);
|
||||
id = id_char;
|
||||
if (data.find(id) == data.end()) {
|
||||
printf("No Info\n");
|
||||
} else {
|
||||
printf("%d\n", data[id]);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue