Cod sursa(job #2414679)

Utilizator Andrei-27Arhire Andrei Andrei-27 Data 24 aprilie 2019 21:33:20
Problema Trie Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.9 kb
/// last try
#include <bits/stdc++.h>
using namespace std;
ifstream in("trie.in");
ofstream out("trie.out");
string s ;
char u [ 25 ] ;
int c ;
struct nod {
    int nr_fii , nr_cuv ;
    nod *son [ 26 ] ;
    nod ()  {
        nr_fii = 0 ;
        nr_cuv = 0 ;
        memset( son , 0 , sizeof(son) ) ;
    }
};
nod *root = new nod ;
void add ( nod *tata , int pos ) {

    if( pos == s.size() )   {
        tata -> nr_cuv ++ ;
        return ;
    }
    if ( tata->son[ s [ pos ] - 'a' ] == 0 )    {
        tata->son[ s [ pos ] - 'a' ] = new nod ;
        tata->nr_fii ++ ;
    }
    add( tata->son[ s[ pos ] - 'a' ] , pos + 1 ) ;
}
bool pop ( nod *tata , int pos )    {
        if ( pos == s.size() )  {
            tata->nr_cuv -- ;
        }   else    {
            if ( pop ( tata->son [ s [ pos ] - 'a' ] , pos + 1 )  )     {
                tata->son[ s [ pos ] - 'a' ] = 0 ;
                tata->nr_fii -- ;
            }
        }
    if ( tata != root && tata->nr_fii == 0 && tata->nr_cuv == 0 )   {
        delete tata ;
        return 1 ;
    }
        return 0 ;
}
int count ( nod *tata , int pos )   {
    if ( pos == s.size() )
        return tata->nr_cuv ;
    if ( tata->son[ s [ pos ] - 'a' ] == 0 ) return 0 ;
    return count ( tata->son[ s [ pos ] - 'a' ] , pos + 1 ) ;
}
int lp ( nod *tata , int pos )  {
    if ( pos == s.size() )  return pos ;
    if ( tata->son[ s [ pos ] - 'a' ] == 0 )    return pos ;
    return lp ( tata->son[ s [ pos ] - 'a' ] , pos + 1 ) ;

}
int main() {
    while ( in >> c )   {
        in.get( u , 25 ) ;
    s = (string) u ;
     if ( c == 0 )  {
        add ( root , 0 ) ;
        continue ;
     }
     if ( c == 1 )  {
        pop ( root , 0 ) ;
        continue ;
     }
     if ( c == 2 )  {
        out  << count ( root , 0 ) << '\n' ;
        continue ;
     }
        out << lp ( root , 0 ) << '\n' ;
    }
}