Pagini recente » Cod sursa (job #1666093) | Cod sursa (job #2393328) | Cod sursa (job #1511739) | Cod sursa (job #1256011) | Cod sursa (job #1521868)
#include<cstdio>
struct eu{int val,nr;
eu* fii[27];
eu(){val=nr=0;
for(int i=0;i<27;i++)
fii[i]=NULL;
}
};
eu* t=new eu;
int nr;
void insert(eu* nod,char *s)
{
if(*s==NULL)
{
nod->val++;
return ;
}
if(nod->fii[*s-'a']==NULL)
{
nod->fii[*s-'a']=new eu;
nod->nr++;
}
insert(nod->fii[*s-'a'],s+1);
}
bool destroy(eu *nod,char *s)
{
if(*s==NULL)
nod->val--;
else
{
if(nod->fii[s[0]-'a']!=NULL&&destroy(nod->fii[*s-'a'],s+1)==1)
{
nod->nr--;
nod->fii[s[0]-'a']=NULL;
}
}
if(nod->nr==0&&nod->val==0&&nod!=t)
{
destroy(nod,s+1);
return 1;
}
return 0;
}
void count(eu* nod,char *s)
{
if(*s==NULL)
{
printf("%d\n",nod->val);
return ;
}
if(nod->fii[s[0]-'a']==NULL)
{
printf("0\n");
return ;
}
count(nod->fii[*s-'a'],s+1);
}
void pref(eu* nod,char *s)
{
if(*s==NULL)
{
printf("%d\n",nr);
return ;
}
if(nod->fii[*s-'a']==NULL)
{
printf("%d\n",nr);
return ;
}
nr++;
pref(nod->fii[*s-'a'],s+1);
}
char s[21],c;
int i,j,k,l,m,n;
int main ()
{
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
while(scanf("%d",&n)!=EOF)
{
scanf("%c",&c);
gets(s);
if(n==0)
insert(t,s);
if(n==1)
destroy(t,s);
if(n==2)
count(t,s);
if(n==3)
{
nr=0;
pref(t,s);
}
}
return 0;
}