Pagini recente » Cod sursa (job #1984116) | Cod sursa (job #1024800) | Istoria paginii runda/oji_2008 | Borderou de evaluare (job #996951) | Cod sursa (job #1802622)
#include<fstream>
using namespace std;
ifstream fin("trie.in");
ofstream fout("trie.out");
struct trie{
int cnt;
int fii;
trie *next[26];
trie(){
cnt = fii = 0;
for( int i = 0; i <= 25; i++ ){
next[i] = 0;
}
}
};
trie *rad;
int op;
char s[25];
void add( trie *nod, char *s ){
if( *s == 0 ){
nod->cnt++;
}else{
if( nod->next[ *s - 'a' ] == NULL ){
nod->next[ *s - 'a' ] = new trie;
nod->fii++;
}
add( nod->next[ *s - 'a' ], s + 1 );
}
}
int det_number( trie *nod, char *s ){
if( *s == 0 ){
return nod->cnt;
}
if( nod->next[ *s - 'a' ] == NULL ){
return 0;
}
return det_number( nod->next[ *s - 'a' ], s + 1 );
}
int del( trie *&nod, char *s ){
if( *s == 0 ){
nod->cnt--;
if( nod->cnt == 0 && nod->fii == 0 && nod != rad ){
delete nod;
nod = 0;
return 1;
}
return 0;
}else{
if( del( nod->next[ *s - 'a' ], s + 1 ) == 1 ){
if( nod->cnt == 0 && nod->fii == 0 && nod != rad ){
delete nod;
nod = 0;
return 1;
}
}
return 0;
}
}
int nr;
int prefmax( trie *nod, char *s ){
nr++;
if( *s == 0 ){
return nr - 1;
}
if( nod->next[ *s - 'a' ] == NULL ){
return nr - 1;
}
return prefmax( nod->next[ *s - 'a' ], s + 1 );
}
int main(){
rad = new trie;
while( fin >> op >> s ){
if( op == 0 ){
add( rad, s );
}
if( op == 1 ){
del( rad, s );
}
if( op == 2 ){
fout << det_number( rad, s ) << "\n";
}
if( op == 3 ){
nr = 0;
fout << prefmax( rad, s ) << "\n";
}
}
return 0;
}