Pagini recente » Cod sursa (job #2267459) | Cod sursa (job #1729724) | Cod sursa (job #965092) | Cod sursa (job #2801367) | Cod sursa (job #800189)
Cod sursa(job #800189)
#include<cstdio>
#include<string.h>
#include<iostream>
#include<vector>
using namespace std;
struct trie
{
int cnt ;
int nrfii ;
trie *l [28] ;
trie ( )
{
cnt=0;
nrfii=0;
memset( l , 0 , sizeof( l ));
}
} ;
trie *t=new trie;
//vector< trie > queue;
int nrapp=0;
void ins ( trie *nod, char *c )
{
if ( *c == '\n' )
{
nod->cnt++ ;
return ;
}
if ( nod->l[*c-'a'] == 0 )
{
nod->l[ *c-'a' ] =new trie ;
nod->nrfii++;
}
ins ( nod->l[*c- 'a'] , c+1 );
}
int del ( trie *nod ,char *c )
{
if( *c == '\n' ) nod->cnt--;
else
{
if( del( nod->l[ *c-'a' ] , c+1 ) )
{
nod->l[ *c-'a' ] = 0 ;
nod->nrfii--;
}
}
if( nod -> cnt == 0 && nod -> nrfii == 0 && nod != t )
{
delete nod;
return 1;
}
return 0;
}
void nr_app( trie *nod , char *c )
{
if( *c == '\n' )
nrapp=nod->cnt ;
else
{
if(nod ->l [*c-'a'] != 0) nr_app ( nod->l[ *c-'a' ] , c+1 ) ;
else nrapp=0;
return ;
}
}
int nr =0 ;
void cml_prefix(trie *nod , char *c)
{
if( *c == '\n' || nod->l[*c-'a'] == 0) return ;
else
cml_prefix ( nod->l[*c-'a'] ,c+1 ) ;
++nr ;
}
//trie queue2;
//void afisare ( )
//{
// while( queue.size() )
/// {
/// queue2 = queue.front() ;
/// printf (" cont=%d nrfii=%d \n ",queue2.cnt,queue2.nrfii);
// queue.erase(queue.begin(),queue.begin()+1);
//
// for (int i=0;i<=26;++i)
// {
// if( queue2.l[ i ] !=0 )
// {
/// printf("pun in coada %c\n" ,char (i+'a'));
// queue.push_back( *queue2.l[ i ] ) ;
// }
//
/// }
//}
//}
int main ( )
{
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
char str[256];
while (fgets(str,32,stdin)>0 )
{
/// printf("----------------------\n\n");
///queue.push_back(*t);
// afisare( );
// printf("----------------------\n\n");
switch ( str [ 0 ] )
{
case '0' :
ins( t , str+2 ) ;
break;
case '1':
del(t,str+2);
break;
case '2':
nrapp=0;
nr_app(t,str+2);
printf("%d\n", nrapp );
break;
case '3':
nr=0;
cml_prefix(t,str+2);
printf( "%d\n",nr );
}
}
return 0;
}