Cod sursa(job #833305)

Utilizator ciorile.chioareBogatu Adrian ciorile.chioare Data 12 decembrie 2012 11:24:50
Problema Trie Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.14 kb
#include<cstdio>

FILE* in;
FILE* out;


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

class Trie {
        private:
                int nrapp;
                int nrchld;
                Trie *chld[26];
        public:
                Trie()
                {
                        nrchld = 0;
                        nrapp = 0;
                }
                void insert(char *s, int n)
                {
                        if(n < 1) {
                                this->nrapp++;
                                return;
                        }
                        if(chld[*s - 'a'] == NULL) {
                                chld[*s - 'a'] = new Trie();
                                this->nrchld++;
                        }
                        else if(chld[*s - 'a']->nrchld < 1 && chld[*s - 'a']->nrapp < 1)
                                this->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);
                        if(chld[*s - 'a']->nrapp < 1 && 
                                        chld[*s - 'a']->nrchld < 1) {
                                nrchld--;
                        }
                }
                int getApp(char *s, int n)
                {
                        if(n < 1) {
                                return nrapp;
                        }
                        if(chld[*s - 'a'] == NULL)
                                return 0;
                        return chld[*s - 'a']->getApp(s + 1, n - 1);
                }
                int prefix(char *s, int n)
                {
                        if(n < 1)
                                return 0;
                        if(chld[*s - 'a'] == NULL || (chld[*s - 'a']->nrchld < 1
                                                && chld[*s - 'a']->nrapp < 1))
                                return 0;
                        return 1 + chld[*s - 'a']->prefix(s + 1, n - 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: //fprintf(stderr, "Remove %s\n", s);
                                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;
}