Advertisement
Void-voiD

Untitled

Dec 31st, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 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. for (int i = 0; i < len; i++) {
  26. y = x->sons[(int)k[i] - 97];
  27. if (y == NULL) return x;
  28. x = y;
  29. }
  30. return x;
  31. }
  32.  
  33. void Insert(struct Elem *t, char *k, int len)
  34. {
  35. struct Elem *x = Descend(t, k, len);
  36. int i = x->len, l, j;
  37. if (i == len) {
  38. if (x->amount != 1) {
  39. x->amount = 1;
  40. struct Elem *y;
  41. while (1) {
  42. y = x->parent;
  43. if (y == NULL) break;
  44. y->all++;
  45. x = y;
  46. }
  47. }
  48. }
  49. else {
  50. struct Elem *now = x;
  51. for (j = i + 1; j <= len; j++) {
  52. struct Elem *y = (struct Elem *)malloc(sizeof(struct Elem));
  53. y->len = j;
  54. if (j == len) y->amount = 1;
  55. else y->amount = 0;
  56. for (l = 0; l < 26; l++) y->sons[l] = NULL;
  57. y->all = 0;
  58. y->parent = x;
  59. x->sons[(int)k[j - 1] - 97] = y;
  60. x->all++;
  61. x = y;
  62. }
  63. while (1) {
  64. x = now->parent;
  65. if (x == NULL) break;
  66. x->all++;
  67. now = x;
  68. }
  69. }
  70. }
  71.  
  72. void Delete(struct Elem *t, char *k, int len)
  73. {
  74. struct Elem *y, *x = Descend(t, k, len);
  75. x->amount = 0;
  76. while (1) {
  77. y = x->parent;
  78. if (y == NULL) break;
  79. y->all--;
  80. x = y;
  81. }
  82. }
  83.  
  84. void SuperFree(struct Elem *t)
  85. {
  86. if (t != NULL) {
  87. for (int i = 0; i < 26; i++) SuperFree(t->sons[i]);
  88. free(t);
  89. }
  90. }
  91.  
  92. int main()
  93. {
  94. int n, i, len;
  95. char cur[8];
  96. char now[100000];
  97. scanf("%d\n", &n);
  98. struct Elem *t = InitTrie();
  99. for (i = 0; i < n; i++) {
  100. scanf("%s ", cur);
  101. if (0 == strcmp(cur, "INSERT")) {
  102. scanf("%s", now);
  103. len = (int)strlen(now);
  104. Insert(t, now, len);
  105. }
  106. if (0 == strcmp(cur, "PREFIX")) {
  107. scanf("%s", now);
  108. len = (int)strlen(now);
  109. struct Elem *x = Descend(t, now, len);
  110. if (x->len < len) printf("0\n");
  111. else printf("%d\n", x->amount + x->all);
  112. }
  113. if (0 == strcmp(cur, "DELETE")) {
  114. scanf("%s", now);
  115. len = (int)strlen(now);
  116. Delete(t, now, len);
  117. }
  118. if (i != n - 1) scanf("\n");
  119. }
  120. SuperFree(t);
  121. return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement