Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- struct elem
- {
- struct elem **arr;
- int pref;
- int x;
- }*t;
- struct elem * buildtree()
- {
- t = (struct elem*)calloc(1, sizeof(struct elem));
- t->arr = (struct elem**)calloc(35, sizeof(struct elem*));
- t->x = 0;
- t->pref = 0;
- return t;
- }
- int insert(struct elem* t, char *s)
- {
- int x = 0;
- if (!*s)
- {
- if (!t->x)
- {
- t->pref++;
- t->x++;
- return 1;
- }
- }
- else
- {
- x = s - 'a';
- if (!t->arr[x])
- t->arr[x] = buildtree();
- struct elem *temp = t->arr[x];
- if (insert(temp, s + 1))
- {
- t->pref++;
- return x + 1;
- }
- }
- }
- int delete(struct elem* t, char *s)
- {
- if (t)
- {
- if (*s != NULL)
- {
- int j = s - 'a';
- struct elem *temp = t->arr[j];
- if (delete(temp, s + 1))
- {
- t->pref--;
- return 1;
- }
- }
- else
- {
- if (t->x)
- {
- t->x--;
- t->pref--;
- return 1;
- }
- }
- }
- return 0;
- }
- void check(char *command, char * w)
- {
- if (command[0] == 'I')
- {
- insert(t, w);
- return 0;
- }
- if (command[0] == 'P')
- {
- int res = pref(t, w);
- printf("%d", res);
- return 0;
- }
- if (command[0] == 'D')
- {
- delete(t, w);
- return 0;
- }
- }
- int pref(struct elem* t, char *s)
- {
- int x = 0;
- if(t)
- {
- if( *s)
- {
- x = s - 'a';
- struct elem *temp = t->arr[x];
- return pref(temp, s + 1);
- }
- else
- return t->pref;
- }
- return 0;
- }
- int main()
- {
- int n;
- scanf("%d", &n);
- t = buildtree();
- for (int i = 0; i < n; i++)
- {
- char *w = (char*)malloc(sizeof(char)*100100);
- char command[7];
- scanf("%s", command);
- scanf("%s", w);
- check(command, w);
- free(w);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement