Cod sursa(job #655684)
#include <stdio.h>
#include <string.h>
#define wL 27
#define q (*w-'a')
struct trie
{
int nod,s;
trie *link[26];
trie()
{
nod=0;
s=0;
memset(link,NULL,sizeof(link));
}
}*T=new trie;
int count;
inline void addw(trie *t,char *w)
{
if(*w=='\n')
{
++t->nod;
return;
}
if(t->link[q]==NULL)
{
++t->s;
t->link[q]=new trie;
}
addw(t->link[q],w+1);
}
inline int delw(trie *t,char *w)
{
if(*w=='\n') --t->nod;
else
if(delw(t->link[q],w+1))
{
--t->s;
t->link[q]=NULL;
}
if(t->s==0&&t->nod==0&&t!=T)
{
delete t;
return 1;
}
return 0;
}
inline int nodw(trie *t,char *w)
{
if(*w=='\n') return t->nod;
if(t->link[q]) return nodw(t->link[q],w+1);
else return 0;
}
inline void prew(trie *t,char *w)
{
if(*w=='\n') return;
if(t->link[q])
{
++count;
prew(t->link[q],w+1);
}
}
int main()
{
char w[wL];
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
while(!feof(stdin))
{
fgets(w,wL,stdin);
if(!feof(stdin))
switch (w[0])
{
case '0':
addw(T,w+2);
break;
case '1':
delw(T,w+2);
break;
case '2':
printf("%d\n",nodw(T,w+2));
break;
case '3':
count=0;
prew(T,w+2);
printf("%d\n",count);
break;
}
}
return 0;
}