Advertisement
Void-voiD

Untitled

Dec 29th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct Elem {
  6. struct Elem *parent;
  7. int amount, len, all;
  8. char *k;
  9. struct Elem *sons[26];
  10. };
  11.  
  12. struct Elem *InitTrie()
  13. {
  14. struct Elem *t = (struct Elem *)malloc(sizeof(struct Elem));
  15. t->amount = 0;
  16. t->parent = NULL;
  17. for (int i = 0; i < 26; i++) t->sons[i] = NULL;
  18. t->len = 0;
  19. t->all = 0;
  20. return t;
  21. }
  22.  
  23. struct Elem *Descend(struct Elem *t, char *k)
  24. {
  25. struct Elem *x = t, *y;
  26. int i = 0, len = (int)strlen(k);
  27. while (i < len) {
  28. y = x->sons[(int)k[i] - 97];
  29. if (y == NULL) return x;
  30. x = y;
  31. i++;
  32. }
  33. return x;
  34. }
  35.  
  36. void Print(struct Elem *t)
  37. {
  38. for (int i = 0; i < 26; i++) {
  39. if (t->sons[i] != NULL) {
  40. printf("%s %d %d\n", t->sons[i]->k, t->sons[i]->amount, t->sons[i]->all);
  41. Print(t->sons[i]);
  42. }
  43. }
  44. }
  45.  
  46. void Insert(struct Elem *t, char *k)
  47. {
  48. struct Elem *x = Descend(t, k);
  49. int j, i = x->len, true = 0, len = (int)strlen(k);
  50. if (i == len) {
  51. if (x->amount == 1) true = 1;
  52. x->amount = 1;
  53. if (true == 0) {
  54. struct Elem *y;
  55. j = i;
  56. while (j > 0) {
  57. y = x->parent;
  58. y->all++;
  59. x = y;
  60. j--;
  61. }
  62. }
  63. }
  64. else {
  65. j = i;
  66. struct Elem *now = x;
  67. while (i < len) {
  68. struct Elem *y = (struct Elem *)malloc(sizeof(struct Elem));
  69. y->len = i + 1;
  70. if (i == len - 1) y->amount = 1;
  71. else y->amount = 0;
  72. for (j = 0; j < 26; j++) y->sons[j] = NULL;
  73. y->all = 0;
  74. char *kk = (char*)malloc((i + 2) * sizeof(char));
  75. for (j = 0; j <= i; j++) kk[j] = k[j];
  76. kk[i + 1] = 0;
  77. y->k = kk;
  78. y->parent = x;
  79. x->sons[(int)k[i] - 97] = y;
  80. x->all++;
  81. x = y;
  82. i++;
  83. }
  84. struct Elem *y;
  85. while (j > 0) {
  86. if (now != NULL) {
  87. y = now->parent;
  88. if (y != NULL) y->all++;
  89. now = y;
  90. }
  91. j--;
  92. }
  93. }
  94. free(k);
  95. }
  96.  
  97. void Delete(struct Elem *t, char *k)
  98. {
  99. struct Elem *y, *x = Descend(t, k);
  100. int true = 0, i = x->len, j;
  101. for (j = 0; j < 26; j++) if (x->sons[j] != NULL && x->sons[j]->amount != 0) true = 1;
  102. if (true == 1) {
  103. x->amount = 0;
  104. while (i > 0) {
  105. y = x->parent;
  106. if (y != NULL) y->all--;
  107. x = y;
  108. i--;
  109. }
  110. }
  111. else {
  112. while (i > 0) {
  113. y = x->parent;
  114. true = 0;
  115. for (j = 0; j < 26; j++) if (x->sons[j] != NULL && x->sons[j]->amount != 0) true = 1;
  116. y->all--;
  117. if (y->amount == 1 || true == 1) break;
  118. x = y;
  119. i--;
  120. }
  121. if (i != 0) {
  122. x = y;
  123. while (i > 0) {
  124. y = x->parent;
  125. if (y != NULL) y->all--;
  126. x = y;
  127. i--;
  128. }
  129. }
  130. }
  131. free(k);
  132. }
  133.  
  134. void SuperFree(struct Elem *t)
  135. {
  136. if (t != NULL) {
  137. for (int i = 0; i < 26; i++) SuperFree(t->sons[i]);
  138. if (t->len > 0 && t->k != NULL) free(t->k);
  139. free(t);
  140. }
  141. }
  142.  
  143. int main()
  144. {
  145. int n, i;
  146. char cur[8];
  147. scanf("%d\n", &n);
  148. struct Elem *t = InitTrie();
  149. for (i = 0; i < n; i++) {
  150. scanf("%s ", cur);
  151. if (0 == strcmp(cur, "INSERT")) {
  152. char *s = (char *)malloc(100000 * sizeof(char));
  153. scanf("%s", s);
  154. Insert(t, s);
  155. }
  156. if (0 == strcmp(cur, "PREFIX")) {
  157. char *s = (char *)malloc(100000 * sizeof(char));
  158. scanf("%s", s);
  159. struct Elem *x = Descend(t, s);
  160. if (x->len != (int)strlen(s)) printf("0\n");
  161. else printf("%d\n", x->amount + x->all);
  162. free(s);
  163. }
  164. if (0 == strcmp(cur, "DELETE")) {
  165. char *s = (char *)malloc(100000 * sizeof(char));
  166. scanf("%s", s);
  167. Delete(t, s);
  168. }
  169. if (i != n - 1) scanf("\n");
  170. }
  171. SuperFree(t);
  172. return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement