Pagini recente » Cod sursa (job #2071958) | Cod sursa (job #31125) | Cod sursa (job #2385037) | Cod sursa (job #2340069) | Cod sursa (job #1290681)
#include <cstdio>
using namespace std;
struct trie
{
trie *s[26];
int v,c;
trie()
{
c=v=0;
for(int i=0;i<26;i++)
s[i]=NULL;
}
};
trie *R,*P;
char w[25],*p;
int a,k;
int main()
{
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
R=new trie;
for(;scanf("%d",&a)+1;)
{
scanf("%s",w);
if(a==0)
{
for(P=R,p=w;*p;p++)
{
if(P->s[*p-'a']==NULL)
P->s[*p-'a']=new trie;
P=P->s[*p-'a'];
P->c++;
}
P->v++;
continue;
}
if(a==1)
{
for(P=R,p=w;*p;p++)
{
P=P->s[*p-'a'];
P->c--;
}
P->v--;
continue;
}
if(a==2)
{
for(P=R,p=w;*p;p++)
{
if(!P->s[*p-'a'])break;
if(P->s[*p-'a']->c==0)break;
P=P->s[*p-'a'];
}
*p?printf("0\n"):printf("%d\n",P->v);
continue;
}
for(k=0,P=R,p=w;*p;p++)
{
if(!P->s[*p-'a'])break;
if(P->s[*p-'a']->c==0)break;
P=P->s[*p-'a'];k++;
}
printf("%d\n",k);
}
return 0;
}