Pagini recente » Cod sursa (job #481299) | Cod sursa (job #1705373) | Cod sursa (job #965047) | Cod sursa (job #1148297) | Cod sursa (job #948408)
Cod sursa(job #948408)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<vector>
#include<unordered_map>
#include<map>
using namespace std;
#define maxl 30
ifstream fin("trie.in") ;
ofstream fout("trie.out") ;
char s[maxl] ;
struct trie
{
int nr, nrfii ;
map<char, trie*> my_map ;
trie()
{
nr = nrfii = 0 ;
}
} *T ;
void adauga(trie *nod, char *s )
{
if( *s > 'z' || *s < 'a' )
{
nod -> nr++ ;
return ;
}
if( nod -> my_map.find(*s) == nod -> my_map.end() ) // nod -> fiu[ *s - 'a' ] == 0 )
{
nod -> my_map.insert( make_pair( *s, new trie ) ) ; //nod -> fiu[ *s - 'a' ] = new trie ;
nod -> nrfii++ ;
}
adauga( nod -> my_map.find(*s) -> second, s + 1 ) ; //adauga( nod -> fiu[ *s - 'a' ], s + 1 ) ;
}
int sterge(trie *nod, char *s )
{
if( *s > 'z' || *s < 'a' )
nod -> nr-- ;
else
if( sterge( nod -> my_map.find(*s) -> second, s + 1 ) == 1 ) //if( sterge( nod-> fiu[ *s - 'a' ], s + 1 ) == 1 )
{
nod -> my_map.erase( *s ) ; //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 -> my_map.find(*s) != nod -> my_map.end() ) //if( nod -> fiu[ *s - 'a' ] )
return aparitii( nod -> my_map.find(*s) -> second, s + 1 ) ; //return aparitii( nod -> fiu[ *s - 'a' ], s + 1 ) ;
return 0 ;
}
int prefix(trie *nod, char *s, int poz)
{
if( *s > 'z' || *s < 'a' || nod -> my_map.find(*s) == nod -> my_map.end() ) //if( *s > 'z' || *s < 'a' || nod -> fiu[ *s - 'a' ] == 0 )
return poz ;
return prefix( nod -> my_map.find(*s) -> second, s + 1, poz + 1 ) ; //return prefix( nod -> fiu[ *s - 'a' ], s + 1, poz + 1 ) ;
}
int main()
{
T = new trie ;
while( fin.getline(s, maxl) )
{
if( s[0] == '0' )
adauga( T, s + 2 ) ;
if( s[0] == '1' )
sterge( T, s + 2 ) ;
if( s[0] == '2' )
fout << aparitii( T, s + 2 ) << '\n' ;
if( s[0] == '3' )
fout << prefix(T, s + 2, 0 ) << '\n' ;
}
return 0 ;
}