Pagini recente » Cod sursa (job #2493065) | Cod sursa (job #1622210) | Cod sursa (job #420780) | Cod sursa (job #2753069) | Cod sursa (job #1060980)
#include <stdio.h>
#include <string.h>
#define fr(i,a,b) for(int i=a;i<b;++i)
#define S 20
#define x (s[0]-'a')
char s[S+1];
struct T{
int b,c;
T*ch[26];
T(){memset(ch,0,sizeof(ch));b=c=0;}
};
T A,*root=&A;
void add(T*t,char*s){
++t->c;
if(!s[0]){++t->b;return;}
if(!t->ch[x])t->ch[x]=new T;
add(t->ch[x],s+1);
}
void del(T*t,char*s){
--t->c;
if(!s[0]){--t->b;return;}
del(t->ch[x],s+1);
if(!t->ch[x]->c){delete t->ch[x];t->ch[x]=(T*)0;}
}
int q(T*t,char*s){
if(!s[0])return t->b;
if(!t->ch[x])return 0;
return q(t->ch[x],s+1);
}
int q2(T*t,char*s){
if(!s[0])return 0;
if(!t->ch[x])return 0;
return 1+q2(t->ch[x],s+1);
}
int main(){
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
int b;
while(!feof(stdin)){
scanf("%i %s\n",&b,s);
if(b==0)add(root,s);
else if(b==1)del(root,s);
else if(b==2)printf("%i\n",q(root,s));
else printf("%i\n",q2(root,s));
}
return 0;
}