Advertisement
Void-voiD

Untitled

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