Void-voiD

Untitled

Dec 28th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 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;
  27. while (i < (int)strlen(k)) {
  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;
  50. if (i == (int)strlen(k)) {
  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 < (int)strlen(k)) {
  68. struct Elem *y = (struct Elem *)malloc(sizeof(struct Elem));
  69. y->len = i + 1;
  70. if (i == (int)strlen(k) - 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) true = 1;
  102. if (true == 1) {
  103. x->amount = 0;
  104. while (i > 0) {
  105. y = x->parent;
  106. y->all--;
  107. x = y;
  108. i--;
  109. }
  110. }
  111. else {
  112. while (i > 0) {
  113. y = x->parent;
  114. i--;
  115. y->sons[(int)k[i] - 97] = NULL;
  116. free(x->k);
  117. free(x);
  118. true = 0;
  119. for (j = 0; j < 26; j++) if (y->sons[j] != NULL) true = 1;
  120. y->all--;
  121. if (y->amount == 1 || true == 1) break;
  122. x = y;
  123. }
  124. if (i != 0) {
  125. x = y;
  126. while (i > 0) {
  127. y = x->parent;
  128. y->all--;
  129. x = y;
  130. i--;
  131. }
  132. }
  133. }
  134. free(k);
  135. }
  136.  
  137. void SuperFree(struct Elem *t)
  138. {
  139. if (t != NULL) {
  140. for (int i = 0; i < 26; i++) SuperFree(t->sons[i]);
  141. if (t->len > 0 && t->k != NULL) free(t->k);
  142. free(t);
  143. }
  144. }
  145.  
  146. int main()
  147. {
  148. int n, i;
  149. char cur[8];
  150. struct Elem *t = InitTrie();
  151. scanf("%d\n", &n);
  152. for (i = 0; i < n; i++) {
  153. scanf("%s ", cur);
  154. if (0 == strcmp(cur, "INSERT")) {
  155. char *s = (char *)malloc(100000 * sizeof(char));
  156. scanf("%s", s);
  157. Insert(t, s);
  158. }
  159. if (0 == strcmp(cur, "PREFIX")) {
  160. char *s = (char *)malloc(100000 * sizeof(char));
  161. scanf("%s", s);
  162. struct Elem *x = Descend(t, s);
  163. if (x->len != (int)strlen(s)) printf("0\n");
  164. else printf("%d\n", x->amount + x->all);
  165. free(s);
  166. }
  167. if (0 == strcmp(cur, "DELETE")) {
  168. char *s = (char *)malloc(100000 * sizeof(char));
  169. scanf("%s", s);
  170. Delete(t, s);
  171. }
  172. if (i != n - 1) scanf("\n");
  173. }
  174. SuperFree(t);
  175. return 0;
  176. }
Add Comment
Please, Sign In to add comment