Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <mem.h>
  4. #include <ctype.h>
  5.  
  6. #define MAX_STR_LEN 1024
  7. #define MAX_HEAP_SIZE 5000
  8. #define MAX_NUM_LNGTH 6
  9. #define ASCII_SIZE 128
  10. #define ALPHABET 36
  11. #define ALPHABET_OFFSET 48
  12.  
  13. typedef struct t_node {
  14.     struct t_node *child[ALPHABET];
  15.     unsigned long long child_mask;
  16.     int code;
  17. } t_node;
  18.  
  19. char STR[MAX_STR_LEN];
  20. size_t STR_LENGTH;
  21. t_node HEAP[MAX_HEAP_SIZE + 1], *last = HEAP;
  22. int IND[ASCII_SIZE];
  23.  
  24.  
  25. t_node *new();
  26. void *delete_all() { last = HEAP; }
  27. void out(FILE *f, t_node *root, int prev_code);
  28.  
  29. int main()
  30. {
  31.     int n;
  32.     unsigned long long _ONE_ = 1;
  33.     FILE *f = fopen("input.txt", "r");
  34.     t_node *root, *curr;
  35.  
  36.     fgets(STR, MAX_NUM_LNGTH, f);
  37.     n = atoi(STR);
  38.  
  39.     int c;
  40.     for (c = '0'; c <= '9'; ++c)
  41.         IND[c] = c - ALPHABET_OFFSET;
  42.     for (int j = 'a'; j <= 'z'; ++j, ++c)
  43.         IND[j] = c - ALPHABET_OFFSET;
  44.  
  45.     int k = 0;
  46.     root = new();
  47.     curr = root;
  48.     while (!feof(f)) {
  49.         memset(STR, 0, MAX_STR_LEN);
  50.         fread(STR, sizeof(char), MAX_STR_LEN, f);
  51.         STR_LENGTH = strlen(STR);
  52.         if (STR[STR_LENGTH - 1] == '\n')
  53.             STR[--STR_LENGTH] = '\0';
  54.  
  55.         for (int i = 0; i < STR_LENGTH; ++i) {
  56.             if (((curr->child_mask >> IND[tolower(STR[i])]) & 1) != 0) {
  57.                 curr = curr->child[IND[tolower(STR[i])]];
  58.                 continue;
  59.             }
  60.  
  61.             if (k < n) {
  62.                 curr->child[IND[tolower(STR[i])]] = new();
  63.                 curr->child_mask |= (_ONE_ << IND[tolower(STR[i])]);
  64.                 curr = curr->child[IND[tolower(STR[i])]];
  65.                 curr->code = k;
  66.                 ++k;
  67.                 curr = root;
  68.                 continue;
  69.             }
  70.  
  71.             delete_all();
  72.             root = new();
  73.             root->child[IND[tolower(STR[i])]] = new();
  74.             root->child_mask |= (_ONE_ << IND[tolower(STR[i])]);
  75.             curr = root;
  76.             curr->code = 0;
  77.             k = 1;
  78.         }
  79.     }
  80.  
  81.     fclose(f);
  82.     f = fopen("output.txt", "w");
  83.  
  84.     for (int i = 0; i < ALPHABET; ++i) {
  85.         if (((root->child_mask >> i) & 1) != 0) {
  86.             STR[0] = (char) ((i <= IND['9']) ? (i + ALPHABET_OFFSET) : (i + ALPHABET_OFFSET + ('a' - '9' - 1)));
  87.             STR[1] = '\0';
  88.             STR_LENGTH = 1;
  89.             out(f, root->child[i], root->child[i]->code);
  90.         }
  91.     }
  92.  
  93.     fclose(f);
  94.  
  95.     return 0;
  96. }
  97.  
  98. t_node *new()
  99. {
  100.     last->child_mask = (unsigned long long)0;
  101.     return last++;
  102. }
  103.  
  104. void out(FILE *f, t_node *root, int prev_code)
  105. {
  106.     fprintf(f, "%s %d\n", STR, prev_code);
  107.     if (root->child_mask != 0) {
  108.         char curr[2];
  109.         for (int i = 0; i < ALPHABET; ++i) {
  110.             if (((root->child_mask >> i) & 1) != 0) {
  111.                 curr[0] = (char)((i <= IND['9']) ? (i + ALPHABET_OFFSET) : (i + ALPHABET_OFFSET + ('a' - '9' - 1)));
  112.                 curr[1] = '\0';
  113.                 strcat(STR, curr);
  114.                 ++STR_LENGTH;
  115.                 out(f, root->child[i], root->child[i]->code);
  116.                 STR[--STR_LENGTH] = '\0';
  117.             }
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement