Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct Elem {
- struct Elem *parent;
- int amount, len, all;
- char *k;
- struct Elem *sons[26];
- };
- struct Elem *InitTrie()
- {
- struct Elem *t = (struct Elem *)malloc(sizeof(struct Elem));
- t->amount = 0;
- t->parent = NULL;
- for (int i = 0; i < 26; i++) t->sons[i] = NULL;
- t->len = 0;
- t->all = 0;
- return t;
- }
- struct Elem *Descend(struct Elem *t, char *k)
- {
- struct Elem *x = t, *y;
- int i = 0, len = (int)strlen(k);
- while (i < len) {
- y = x->sons[(int)k[i] - 97];
- if (y == NULL) return x;
- x = y;
- i++;
- }
- return x;
- }
- void Print(struct Elem *t)
- {
- for (int i = 0; i < 26; i++) {
- if (t->sons[i] != NULL) {
- printf("%s %d %d\n", t->sons[i]->k, t->sons[i]->amount, t->sons[i]->all);
- Print(t->sons[i]);
- }
- }
- }
- void Insert(struct Elem *t, char *k)
- {
- struct Elem *x = Descend(t, k);
- int j, i = x->len, true = 0, len = (int)strlen(k);
- if (i == len) {
- if (x->amount == 1) true = 1;
- x->amount = 1;
- if (true == 0) {
- struct Elem *y;
- j = i;
- while (j > 0) {
- y = x->parent;
- y->all++;
- x = y;
- j--;
- }
- }
- }
- else {
- j = i;
- struct Elem *now = x;
- while (i < len) {
- struct Elem *y = (struct Elem *)malloc(sizeof(struct Elem));
- y->len = i + 1;
- if (i == len - 1) y->amount = 1;
- else y->amount = 0;
- for (j = 0; j < 26; j++) y->sons[j] = NULL;
- y->all = 0;
- char *kk = (char*)malloc((i + 2) * sizeof(char));
- for (j = 0; j <= i; j++) kk[j] = k[j];
- kk[i + 1] = 0;
- y->k = kk;
- y->parent = x;
- x->sons[(int)k[i] - 97] = y;
- x->all++;
- x = y;
- i++;
- }
- struct Elem *y;
- while (j > 0) {
- if (now != NULL) {
- y = now->parent;
- if (y != NULL) y->all++;
- now = y;
- }
- j--;
- }
- }
- free(k);
- }
- void Delete(struct Elem *t, char *k)
- {
- struct Elem *y, *x = Descend(t, k);
- int true = 0, i = x->len, j;
- for (j = 0; j < 26; j++) if (x->sons[j] != NULL) true = 1;
- if (true == 1) {
- x->amount = 0;
- while (i > 0) {
- y = x->parent;
- y->all--;
- x = y;
- i--;
- }
- }
- else {
- while (i > 0) {
- y = x->parent;
- i--;
- y->sons[(int)k[i] - 97] = NULL;
- free(x->k);
- free(x);
- true = 0;
- for (j = 0; j < 26; j++) if (y->sons[j] != NULL) true = 1;
- y->all--;
- if (y->amount == 1 || true == 1) break;
- x = y;
- }
- if (i != 0) {
- x = y;
- while (i > 0) {
- y = x->parent;
- y->all--;
- x = y;
- i--;
- }
- }
- }
- free(k);
- }
- void SuperFree(struct Elem *t)
- {
- if (t != NULL) {
- for (int i = 0; i < 26; i++) SuperFree(t->sons[i]);
- if (t->len > 0 && t->k != NULL) free(t->k);
- free(t);
- }
- }
- int main()
- {
- int n, i;
- char cur[8];
- struct Elem *t = InitTrie();
- scanf("%d\n", &n);
- for (i = 0; i < n; i++) {
- scanf("%s ", cur);
- if (0 == strcmp(cur, "INSERT")) {
- char *s = (char *)malloc(100000 * sizeof(char));
- scanf("%s", s);
- Insert(t, s);
- }
- if (0 == strcmp(cur, "PREFIX")) {
- char *s = (char *)malloc(100000 * sizeof(char));
- scanf("%s", s);
- struct Elem *x = Descend(t, s);
- if (x->len != (int)strlen(s)) printf("0\n");
- else printf("%d\n", x->amount + x->all);
- free(s);
- }
- if (0 == strcmp(cur, "DELETE")) {
- char *s = (char *)malloc(100000 * sizeof(char));
- scanf("%s", s);
- Delete(t, s);
- }
- if (i != n - 1) scanf("\n");
- }
- SuperFree(t);
- return 0;
- }
Add Comment
Please, Sign In to add comment