Cod sursa(job #1998120)

Utilizator Victor24Vasiesiu Victor Victor24 Data 6 iulie 2017 16:56:32
Problema Trie Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.84 kb
#include <bits/stdc++.h>
#define l s[poz]-96
using namespace std;

struct trie
{
    int cuv, ex[27];
    trie *fii[27];
    trie()
    {
        this->cuv = 0;
        memset(fii, 0, sizeof(fii));
        memset(ex, 0, sizeof(ex));
    }
};

ifstream fin ("trie.in");
ofstream fout ("trie.out");

int op;

string s;

void add (trie *&nod , int poz)
{
    if ( poz == s.size() )
    {
        nod->cuv++;
        return;
    }
    if (nod->fii[l] == 0)
    {
        nod->fii[l] = new trie;
    }
    nod->ex[l]++;
    add ( nod->fii[l] , poz+1 );
}

void del ( trie *&nod , int poz )
{
    if ( poz == s.size() )
    {
        nod->cuv--;
        return;
    }

    nod->ex[l]--;

    del ( nod->fii[l] , poz+1 );

}

int nrap ( trie *&nod, int poz )
{
    if ( poz == s.size() )
    {
        return nod->cuv;
    }

    if ( nod->ex[l] != 0 )
    {
        return nrap ( nod->fii[l] , poz+1 );
    }

    return 0;

}

int lunpf ( trie *&nod , int poz )
{
    if ( poz == s.size() )
    {
        return 0;
    }
    if ( nod->ex[l] != 0 )
    {
        return 1 + lunpf ( nod->fii[l] , poz+1 );
    }

    return 0;
}

int main ()
{
    trie *t = new trie;

    while ( fin>>op )
    {
        fin>>s;

        switch (op)
        {
            case 0:
                {
                    add( t , 0 );
                    break;
                }
            case 1:
                {
                    del ( t , 0 );
                    break;
                }
            case 2:
                {
                    fout<< nrap ( t, 0 ) << '\n';
                    break;
                }
            case 3:
                {
                    fout<< lunpf ( t, 0 ) << '\n';
                    break;
                }
        }
    }

    return 0;
}