Advertisement
Void-voiD

Untitled

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