Pagini recente » Monitorul de evaluare | Cod sursa (job #1102309) | Cod sursa (job #2453853) | Cod sursa (job #1475646) | Cod sursa (job #1998121)
#include <bits/stdc++.h>
#define l s[poz]-96
using namespace std;
struct trie
{
int cuv, nrf;
trie *fii[27];
trie()
{
this->cuv = 0;
memset(fii, 0, sizeof(fii));
nrf = 0;
}
};
ifstream fin ("trie.in");
ofstream fout ("trie.out");
int op;
string s;
void add (trie *&nod , int poz)
{
if ( poz == s.size() )
{
nod->cuv++;
return;
}
if (nod->fii[l] == 0)
{
nod->fii[l] = new trie;
}
nod->nrf++;
add ( nod->fii[l] , poz+1 );
}
void del ( trie *&nod , int poz )
{
if ( poz == s.size() )
{
nod->cuv--;
return;
}
nod->nrf--;
del ( nod->fii[l] , poz+1 );
}
int nrap ( trie *&nod, int poz )
{
if ( poz == s.size() )
{
return nod->cuv;
}
if ( nod->fii[l] != 0 && (nod->fii[l]->nrf || nod->fii[l]->cuv ) )
{
return nrap ( nod->fii[l] , poz+1 );
}
return 0;
}
int lunpf ( trie *&nod , int poz )
{
if ( poz == s.size() )
{
return 0;
}
if ( nod->fii[l] != 0 && (nod->fii[l]->nrf || nod->fii[l]->cuv ) )
{
return 1 + lunpf ( nod->fii[l] , poz+1 );
}
return 0;
}
int main ()
{
trie *t = new trie;
while ( fin>>op )
{
fin>>s;
switch (op)
{
case 0:
{
add( t , 0 );
break;
}
case 1:
{
del ( t , 0 );
break;
}
case 2:
{
fout<< nrap ( t, 0 ) << '\n';
break;
}
case 3:
{
fout<< lunpf ( t, 0 ) << '\n';
break;
}
}
}
return 0;
}