Advertisement
Void-voiD

Untitled

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