Cod sursa(job #2487098)

Utilizator verde.cristian2005Verde Flaviu-Cristian verde.cristian2005 Data 3 noiembrie 2019 22:20:00
Problema Trie Scor 55
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.65 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in ( "trie.in" );
ofstream out ( "trie.out" );
struct nod
{
    int info;
    nod *urm[26], *prec;
    /*nod() {
        info = 0;
        memset ( urm, 0, sizeof ( urm ) );
        memset ( prec, 0, sizeof ( prec ) );
    }*/
};
nod* prim;
void adaug ( nod* &first, string s )
{
    if ( first == NULL )
    {
        first = new nod;
        first->info = 0;
        for ( int i = 0; i < 26; i++ )
            first->urm[i] = NULL;
        first->prec=NULL;
    }
    nod *aux = first;
    for ( int i = 0; i < s.size(); i++ )
    {
        if ( aux->urm[s[i] - 'a'] == NULL )
        {
            nod *aux2 = new nod;
            aux2->info=0;
            for(int k=0;k<26;k++)
                aux2->urm[k]=NULL;
            aux->urm[s[i] - 'a'] = aux2;
            aux2->prec= aux;
            aux = aux2;
            for ( int j = i + 1; j < s.size(); j++ )
            {
                aux2 = new nod;
                aux2->info = 0;
                for ( int k = 0; k < 26; k++ )
                    aux2->urm[k] = NULL;
                aux2->prec=aux;
                aux->urm[s[j] - 'a'] = aux2;
                aux = aux2;
            }
            break;
        }
        else
            aux = aux->urm[s[i] - 'a'];
    }
    aux->info++;
}
void scoate ( nod* &first, string s )
{
    nod *aux = first;
    for ( int i = 0; i < s.size(); i++ )
        aux = aux->urm[s[i] - 'a'];
    if ( aux->info >= 2 )
        aux->info--;
    else
    {
        aux->info--;
        /*nod *aux2;
        for ( int i = s.size() - 1; i >= 2 && !aux->info; i-- )
        {
            aux2 = aux->prec;
            aux2->urm[s[i] - 'a'] = NULL;
            delete ( aux );
            aux = aux2;
        }*/
    }
}
void afiseaza ( nod* &first, string s )
{
    nod *aux = first;
    for ( int i = 0; i < s.size()&&aux!=NULL; i++ )
        aux = aux->urm[s[i] - 'a'];
        if(aux!=NULL)
    out << aux->info << '\n';
    else
        out<<"0\n";
}
void prefix ( nod* &first, string s )
{
    nod *aux = first;
    int cnt = 0,i=0;
    for ( ; i < s.size() && aux != NULL; i++ )
        aux = aux->urm[s[i] - 'a'];
    if(aux!=NULL)
        out<<s.size()<<'\n';
    else
        out<<i-1<<'\n';
}
string s, s0;
int main()
{
    int t;
    //prim = new nod();
    for(string s;in>>t;)
    {
        in>>s;
        if ( t == 0 )
            adaug ( prim, s );
        else if ( t == 1 )
            scoate ( prim, s );
        else if ( t == 2 )
            afiseaza ( prim, s );
        else
            prefix ( prim, s );
    }
    return 0;
}