Advertisement
xxZeus

huff man try

Dec 5th, 2020
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5.  
  6. // @author: Simran Singh 1816410280
  7. struct node {
  8.   int f;
  9.   char c;
  10.   struct node *left;
  11.   struct node *right;
  12. };
  13. void swap(struct node *a, struct node *b) {
  14.     struct node temp = *a;
  15.     *a = *b;
  16.     *b = temp;
  17. }
  18. struct node* nodeFromTwo(struct node *a, struct node *b) {
  19.     struct node* node = (struct node*)calloc(1, sizeof(struct node));
  20.     node->f = a->f + b->f;
  21.     node->c = '*';
  22.     node->left = a;
  23.     node->right = b;
  24.     //printf("Summing %c %c = %d\n", a->c, b->c, node->f);    
  25.     return(node);
  26. }
  27. char cMap[26][100], sMap[26][100];
  28. void createMap(struct node *n, char *str) {
  29.     if(n->left == NULL) {
  30.         if(n->c >= 65 && n->c <= 65+26)
  31.             strcpy(cMap[n->c - 65], str);
  32.         else
  33.             strcpy(sMap[n->c - 97], str);
  34.         return;
  35.     }
  36.    
  37.     char *left = (char*)malloc(26*sizeof(char));
  38.     char *right = (char*)malloc(26*sizeof(char));
  39.     strcpy(left, str);
  40.     strcpy(right, str);
  41.     int len = strlen(str);
  42.     left[len] = '0';
  43.     right[len] = '1';
  44.     left[len+1] = '\0';
  45.     right[len+1] = '\0';
  46.     free(str);
  47.     //printf("%c\n",n->c);
  48.     createMap(n->left, left);
  49.     createMap(n->right, right);
  50. }
  51. int main() {    
  52.     int n, q, i, j, qtype[9], sum = 0;
  53.     struct node cache[100], *temp;
  54.     char queries[9][1501];
  55.     scanf("%d %d", &n, &q);
  56.     for(i = 0; i < n; i++) {
  57.         scanf("\n%c %d", &cache[i].c, &cache[i].f);
  58.         sum += cache[i].f;
  59.         cache[i].left = NULL;
  60.         cache[i].right = NULL;
  61.     }
  62.     for(i = 0; i < q; i++) {
  63.         scanf("%d %s", qtype + i, queries[i]);        
  64.     }
  65.     for(i = 0; i < n; i++) {
  66.         for(j = 0; j < n-i-1; j++) {
  67.             if(cache[j].f > cache[j+1].f) {
  68.                 swap(&cache[j], &cache[j+1]);
  69.             }
  70.         }
  71.     }
  72.     for(i = 0; i < n; i+=2) {
  73.         temp = nodeFromTwo(&cache[i], &cache[i+1]);
  74.         if(temp->f == sum)
  75.             break;
  76.         j = n-1;
  77.         while(cache[j].f >= temp->f){
  78.             cache[j+1] = cache[j];
  79.             j--;
  80.         }
  81.         cache[j+1] = *temp;
  82.         n++;
  83.     }
  84.     struct node *root = temp;
  85.     char *str = (char*)malloc(sizeof(char));
  86.     str[0] = '\0';
  87.     char c;
  88.     createMap(temp, str);
  89.     for(i = 0; i < q; i++) {
  90.         j = 0;
  91.         if(qtype[i] == 1) {  
  92.             c = *(queries[i] + j);
  93.             while(c != '\0') {        
  94.                 if(c >= 65 && c <= 65+26)
  95.                     printf("%s", cMap[c - 65]);
  96.                 else
  97.                     printf("%s", sMap[c - 97]);
  98.                 j++;  
  99.                 c = *(queries[i] + j);
  100.             }
  101.             printf("\n");
  102.         }
  103.         else {  
  104.             c = *(queries[i] + j);
  105.             temp = root;
  106.             while(c != '\0') {        
  107.                 if(temp->left != NULL) {
  108.                     if(c == '0')
  109.                         temp = temp->left;
  110.                     else
  111.                         temp = temp->right;
  112.                 }
  113.                 if(temp->left == NULL) {
  114.                     printf("%c", temp->c);
  115.                     temp = root;
  116.                 }
  117.                 j++;  
  118.                 c = *(queries[i] + j);
  119.             }
  120.             printf("\n");
  121.         }
  122.        
  123.     }
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement