Pagini recente » Cod sursa (job #241905) | Cod sursa (job #267322) | Cod sursa (job #903686) | Cod sursa (job #2474687) | Cod sursa (job #765397)
Cod sursa(job #765397)
#include<cstdio>
typedef struct nod
{int c,n;
struct nod *f[26];}Nod;
int p;
char l[32];
Nod *B()
{Nod *t=new Nod;
t->c=t->n=0;
for(int i=0;i<26;i++)
t->f[i]=NULL;
return t;}
Nod *t=B();
void A(Nod *s,char *l)
{if((*l)=='\0')
{++s->c;
return;}
if(!s->f[(*l)-'a'])
s->f[(*l)-'a']=B(),++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)
t=B(),D(t,l);
if(p==2)
printf("%d\n",C(t,l));
if(p==3)
printf("%d\n",E(t,l,0));}
return 0;}