Cod sursa(job #2368519)

Utilizator AplayLazar Laurentiu Aplay Data 5 martie 2019 16:27:41
Problema Trie Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.61 kb
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define ADD 0
#define DELETE 1
#define COUNT 2
#define LONGEST_PREFIX 3

#define DEBUG 0

struct node {
    short pass, end;
    struct node **next;
};

char word[21];
int operation;
struct node* trie[26];

struct node* makeNode(char value) {
    struct node* node = (struct node*) malloc(sizeof(struct node));
    node->pass = 0;
    node->end = 0;
    node->next = (struct node**) calloc(26, sizeof(struct node*));
    return node;
}

int main() {
    freopen("trie.in", "r", stdin);
    freopen("trie.out", "w", stdout);

    while (EOF != scanf("%d %s\n", &operation, word)) {
        switch (operation) {
            case ADD: {
                if (DEBUG) printf("--> ADD: %s\n", word);
                int length = strlen(word);
                struct node** next = trie;
                for (int it = 0; it < length; ++it) {
                    int index = word[it] - 'a';

                    if (DEBUG) printf("   %c -> %d\n", word[it], NULL == next[index]);

                    if (NULL == next[index]) {
                        next[index] = makeNode(word[it]);
                    }

                    ++(next[index]->pass);
                    if (length - 1 == it) ++(next[index]->end);
                    next = next[index]->next;
                }

                break;
            }
            case DELETE: {
                if (DEBUG) printf("--> DELETE: %s\n", word);
                int length = strlen(word);
                struct node **next = trie;
                bool nok = false;
                for (int it = 0; it < length - 1; ++it) {
                    int index = word[it] - 'a';
                    if (NULL == next[index]) {
                        nok = true;
                        break;
                    }
                    --(next[index]->pass);
                    next = next[index]->next;
                }

                int lastIndex = word[length - 1] - 'a';
                if (!nok && NULL != next[lastIndex]) {
                    --(next[lastIndex]->pass);
                    --(next[lastIndex]->end);
                }

                break;
            }
            case COUNT: {
                if (DEBUG) printf("--> COUNT: %s\n", word);
                int length = strlen(word);
                struct node **next = trie;
                bool nok = false;
                for (int it = 0; it < length - 1; ++it) {
                    int index = word[it] - 'a';

                    if (DEBUG) printf("   %c -> %d\n", word[it], NULL == next[index]);

                    if (NULL == next[index]) {
                        nok = true;
                        break;
                    }
                    next = next[index]->next;
                }

                int lastIndex = word[length - 1] - 'a';
                if (nok || NULL == next[lastIndex]) {
                    printf("0\n");
                } else {
                    printf("%d\n", next[lastIndex]->end);
                }

                break;
            }
            case LONGEST_PREFIX: {
                if (DEBUG) printf("--> LONGEST PREFIX: %s\n", word);
                int length = strlen(word);
                struct node **next = trie;
                int count = 0;
                int wordIndex = 0;
                while (wordIndex < length && NULL != next[word[wordIndex] - 'a'] && 0 < next[word[wordIndex] - 'a']->pass) {
                    ++count;
                    next = next[word[wordIndex] - 'a']->next;
                     ++wordIndex;
                }
                printf("%d\n", count);
                break;
            }
        }
    }

    return 0;
}