Cod sursa(job #244666)
#include <stdio.h>
#include <string.h>
long t,l,q,ok; char ch[32];
struct trie{int f[26];int cuv;int son;};
trie next[100002];
void add(int nod, char ch[32]){
if (ch[0]==0){next[nod].cuv++;return;}
long k=ch[0]-'a';
if (next[nod].f[k]==0){next[nod].f[k]=++q; next[nod].son++;}
add(next[nod].f[k],ch+1);
}
int del(int nod, char ch[32]){
if (ch[0]==0) next[nod].cuv--;
else{
long k=ch[0]-'a';
if (del(next[nod].f[k],ch+1)){
next[nod].f[k];
next[nod].son--;
}
//if (next[nod].cuv==0 && next[nod].son==0 && nod!=1)
}
}
int count(int nod, char ch[32]){
if (ch[0]==0)return next[nod].cuv;
long k=ch[0]-'a';
if (next[nod].f[k])return count(next[nod].f[k],ch+1);
return 0;
}
int prefix(int nod, int l,char ch[32]){
if (ch[0]==0 || next[nod].f[ch[0]-'a']==0)return l;
return prefix(next[nod].f[ch[0]-'a'],l+1,ch+1);
}
int main(){
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
while(1){
t=-1; scanf("%ld %s\n",&t,ch); if (t==-1)break;
if (t==0)add(1,ch);
if (t==1)del(1,ch);
if (t==2)printf("%ld\n",count(1,ch));
if (t==3)printf("%ld\n",prefix(1,0,ch));
}
return 0;
}