Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.08 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5.  
  6. typedef struct Trie {
  7.     char letter;
  8.     bool is_final;
  9.     struct Trie* child;
  10.     struct Trie* next;
  11. } Trie;
  12.  
  13. void init_trie(Trie** root) {
  14.     *root = (Trie*)calloc(1, sizeof(Trie));
  15. }
  16.  
  17. void add_word(Trie** root, char* word) {
  18.     int i = 0;
  19.     Trie* cur_trie = *root;
  20.     for (i = 0; i < strlen(word); ++i) {
  21.         if (cur_trie->child == NULL) {
  22.             cur_trie->child = (Trie*)calloc(1, sizeof(Trie));
  23.             cur_trie->child->letter = word[i];
  24.             cur_trie = cur_trie->child;
  25.         }
  26.         else {
  27.             Trie* last_trie = cur_trie->child;
  28.             cur_trie = cur_trie->child;
  29.             while (cur_trie && cur_trie->letter != word[i]) {
  30.                 last_trie = cur_trie;
  31.                 cur_trie = cur_trie->next;
  32.             }
  33.             if (cur_trie == NULL) {
  34.                 last_trie->next = (Trie*)calloc(1, sizeof(Trie));
  35.                 cur_trie = last_trie->next;
  36.                 cur_trie->letter = word[i];
  37.             }
  38.         }
  39.     }
  40.     cur_trie->is_final = true;
  41. }
  42.  
  43.  
  44. int contains(Trie* root, char* word) {
  45.     int i = 0;
  46.     Trie* cur_trie = root;
  47.     for (i = 0; i < strlen(word); ++i) {
  48.         if (cur_trie->child == NULL) {
  49.             return 0;
  50.         }
  51.         else {
  52.             cur_trie = cur_trie->child;
  53.             while (cur_trie && cur_trie->letter != word[i]) {
  54.                 cur_trie = cur_trie->next;
  55.             }
  56.             if (cur_trie == NULL) {
  57.                 return 0;
  58.             }
  59.         }
  60.     }
  61.     return cur_trie->is_final == true;
  62. }
  63.  
  64.  
  65. void remove_word(Trie** root, char* word) {
  66.     int i = 0;
  67.     Trie* cur_trie = *root;
  68.     for (i = 0; i < strlen(word); ++i) {
  69.         if (cur_trie->child == NULL) {
  70.             return;
  71.         }
  72.         else {
  73.             cur_trie = cur_trie->child;
  74.             while (cur_trie && cur_trie->letter != word[i]) {
  75.                 cur_trie = cur_trie->next;
  76.             }
  77.             if (cur_trie == NULL) {
  78.                 return;
  79.             }
  80.         }
  81.     }
  82.     cur_trie->is_final = false;
  83. }
  84.  
  85. char* longest_word_helper(Trie* root, int is_root) {
  86.     if (root == NULL) {
  87.         return NULL;
  88.     }
  89.     char* word = NULL;
  90.     if (root->child == NULL) {
  91.         if (is_root || root->is_final == false) {
  92.             return NULL;
  93.         }
  94.         word = (char *)malloc(2);
  95.         word[0] = root->letter;
  96.         word[1] = '\0';
  97.         return word;
  98.     }
  99.     int max_len = -1;
  100.     char* cur_word = NULL;
  101.     Trie* cur_tree = root->child;
  102.     while (cur_tree) {
  103.         char* w = longest_word_helper(cur_tree, 0);
  104.         int len = w == NULL ? 0 : strlen(w);
  105.         if (len > max_len) {
  106.             max_len = len;
  107.             cur_word = w;
  108.         }
  109.         cur_tree = cur_tree->next;
  110.     }
  111.     if (cur_word) {
  112.         if (is_root) {
  113.             return cur_word;
  114.         }
  115.         word = (char *)malloc(max_len + 2);
  116.         word[0] = root->letter;
  117.         memcpy(word + 1, cur_word, max_len);
  118.         word[max_len + 1] = '\0';
  119.         return word;
  120.     }
  121.     else {
  122.         if (root || root->is_final == false) {
  123.             return NULL;
  124.         }
  125.         word = (char*)malloc(2);
  126.         word[0] = root->letter;
  127.         word[1] = '\0';
  128.         return word;
  129.     }
  130.     return NULL;
  131. }
  132.  
  133. char* longest_word(Trie* root) {
  134.     return longest_word_helper(root, 1);
  135. }
  136.  
  137. int main()
  138. {
  139.     Trie* t = NULL;
  140.     init_trie(&t);
  141.     add_word(&t, "vladimir");
  142.     add_word(&t, "ura");
  143.     add_word(&t, "vladimir");
  144.     add_word(&t, "ppp");
  145.     add_word(&t, "aaa");
  146.     add_word(&t, "uradsad");
  147.     add_word(&t, "facultate");
  148.     add_word(&t, "sd");
  149.     remove_word(&t, "aaa");
  150.     printf("%d ", contains(t, "facultate"));
  151.     printf("%d ", contains(t, "ppp"));
  152.     printf("%d ", contains(t, "pppp"));
  153.     printf("%d ", contains(t, "ura"));
  154.     printf("%d ", contains(t, "ura1"));
  155.     printf("%d ", contains(t, "vladimir"));
  156.     printf("%d ", contains(t, "vladimir1"));
  157.     printf("%s ", longest_word(t));
  158.     return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement