Cod sursa(job #1998121)

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

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

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->nrf++;
    add ( nod->fii[l] , poz+1 );
}

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

    nod->nrf--;

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

}

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

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

    return 0;

}

int lunpf ( trie *&nod , int poz )
{
    if ( poz == s.size() )
    {
        return 0;
    }
    if ( nod->fii[l] != 0 && (nod->fii[l]->nrf || nod->fii[l]->cuv )  )
    {
        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;
}