Pagini recente » Cod sursa (job #3181910) | Cod sursa (job #765399)
Cod sursa(job #765399)
#include<cstdio>
#include<cstring>
struct Nod
{int c,n;
Nod *f[26];
Nod()
{c=n=0;
memset(f,0,sizeof(f));}};
int p;
char l[32];
Nod *t=new Nod;
void A(Nod *&s,char *l)
{if((*l)=='\0')
{++s->c;
return;}
if(!s->f[(*l)-'a'])
s->f[(*l)-'a']=new Nod,++s->n;
A(s->f[(*l)-'a'],l+1);}
int D(Nod *&s,char *l)
{if((*l)=='\0')
--s->c;
else
if(D(s->f[(*l)-'a'],l+1))
s->f[(*l)-'a']=0,--s->n;
if(!(s->c)&&!(s->n)&&s!=t)
{delete s;
return 1;}
return 0;}
int C(Nod *&s,char *l)
{if((*l)=='\0')
return s->c;
if(s->f[(*l)-'a'])
return C(s->f[(*l)-'a'],l+1);
return 0;}
int E(Nod *&s,char *l,int k)
{if((*l)=='\0'||!s->f[(*l)-'a'])
return k;
return E(s->f[(*l)-'a'],l+1,k+1);}
int main()
{freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
while(!feof(stdin))
{printf("%d ",&p),gets(l);
if(feof(stdin))
break;
if(!p)
A(t,l);
if(p==1)
D(t,l);
if(p==2)
printf("%d\n",C(t,l));
if(p==3)
printf("%d\n",E(t,l,0));}
return 0;}