Pagini recente » Cod sursa (job #1119338) | Cod sursa (job #841456) | Cod sursa (job #1281310) | Cod sursa (job #1809928) | Cod sursa (job #819042)
Cod sursa(job #819042)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<vector>
using namespace std;
#define maxl 30
char s[maxl] ;
struct trie
{
int nr, nrfii ;
trie *fiu[ maxl ];
trie()
{
nr = nrfii = 0 ;
memset( fiu, 0, sizeof(fiu) ) ;
}
} *T ;
void adauga(trie *nod, char *s )
{
if( *s > 'z' || *s < 'a' )
{
nod -> nr++ ;
return ;
}
if( nod -> fiu[ *s - 'a' ] == 0 )
{
nod -> fiu[ *s - 'a' ] = new trie ;
nod -> nrfii++ ;
}
adauga( nod -> fiu[ *s - 'a' ], s + 1 ) ;
}
int sterge(trie *nod, char *s )
{
if( *s > 'z' || *s < 'a' )
nod -> nr-- ;
else
if( sterge( nod-> fiu[ *s - 'a' ], s + 1 ) == 1 )
{
nod -> fiu[ *s - 'a' ] = 0 ;
nod -> nrfii-- ;
}
if( nod -> nr == 0 && nod -> nrfii == 0 && nod != T )
{
delete nod ;
return 1 ;
}
return 0 ;
}
int aparitii(trie *nod,char *s)
{
if( *s > 'z' || *s < 'a' )
return nod -> nr ;
if( nod -> fiu[ *s - 'a' ] )
return aparitii( nod -> fiu[ *s - 'a' ], s + 1 ) ;
return 0 ;
}
int prefix(trie *nod, char *s, int poz)
{
if( *s > 'z' || *s < 'a' || nod -> fiu[ *s - 'a' ] == 0 )
return poz ;
return prefix( nod -> fiu[ *s - 'a' ], s + 1, poz + 1 ) ;
}
int main()
{
//freopen("trie.in", "r", stdin);
//freopen("trie.out", "w", stdout);
ifstream f("trie.in") ;
ofstream g("trie.out") ;
T = new trie ;
while( cin>>s )
{
if( s[0] == '0' )
adauga( T, s + 2 ) ;
if( s[0] == '1' )
sterge( T, s + 2 ) ;
if( s[0] == '2' )
g<< aparitii( T, s + 2 ) << '\n' ;
if( s[0] == '3' )
g<< prefix(T, s + 2, 0 ) << '\n' ;
}
return 0 ;
}