Cod sursa(job #824260)

Utilizator ciorile.chioareBogatu Adrian ciorile.chioare Data 26 noiembrie 2012 08:24:31
Problema Trie Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 3.07 kb
#include<cstdio>

FILE* in;
FILE* out;


int max(int a, int b)
{
        return (a < b)?b:a;
}

class Trie {
        private:
                bool exists;
                int nrapp;
                int nrchld;
                Trie *chld[26];
        public:
                Trie()
                {
                        exists = true;
                        nrchld = 0;
                        nrapp = 0;
                }
                /*
                Trie(int nc, int n a)
                {
                        nrchld = nc;
                        nrapp = a;
                }*/
                void insert(char *s, int n)
                {
                        if(n < 1) {
                                this->nrapp++;
                                return;
                        }
                        if(chld[*s - 'a'] == NULL || !chld[*s - 'a']->exists) {
                                chld[*s - 'a'] = new Trie();
                                nrchld++;
                        }
                        chld[*s - 'a']->insert(s + 1, n - 1);
                }
                void remove(char *s, int n)
                {
                        if(n < 1) {
                                this->nrapp--;
                                return;
                        }
                        chld[*s - 'a']->remove(s + 1, n - 1);
                }
                int getApp(char *s, int n)
                {
                        if(n < 1) {
                                return nrapp;
                        }
                        if(chld[*s - 'a'] == NULL || !chld[*s - 'a']->exists)
                                return 0;
                        return chld[*s - 'a']->getApp(s + 1, n - 1);
                }
                int prefix(char *s, int n)
                {
                        Trie *aux = this;
                        int i;

                        for(i = 0; aux && (aux->nrapp ||
                                                aux->nrchld); i++) {
                                aux = aux->chld[*s - 'a'];
                                s++;
                                n--;
                        }
                        return i - 1;
                }

};

int length(char *s) {
        if(*s == '\0')
                return 0;
        return 1 + length(s + 1);
}

int main()
{
        int x;
        char s[32];
        int n;

        in = fopen("trie.in", "r");
        out = fopen("trie.out", "w");

        Trie *trie = new Trie();

        fscanf(in, "%d %s", &x, s);
        while(!feof(in)) {
                n = length(s);
                switch(x) {
                        case 0: trie->insert(s, n);
                                break;
                        case 1: trie->remove(s, n);
                                break;
                        case 2: fprintf(out, "%d\n", trie->getApp(s, n));
                                break;
                        case 3: fprintf(out, "%d\n", trie->prefix(s, n));
                                break;
                }
                fscanf(in, "%d %s", &x, s);
        }

        return 0;
}