Cod sursa(job #609966)
#include<cstdio>
#include<cstring>
struct trie
{
int S,C;
trie *N[26];
trie()
{
C=S=0;
for(int i=0;i<26;i++)N[i]=0;
}
};
trie *R,*Q,*X,*ST[30];
int x,i,j,LG;
char L[30];
void INIT();
int main()
{
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
R=new trie;R->S++;
for(;;)
{
if(scanf("%d",&x)==-1)break;
scanf("%s",L);LG=strlen(L);
INIT();
if(!x){for(i=0;i<LG;i++)ST[i]->S++;ST[LG]->C++;}
if(x==1){for(i=0;i<LG;i++)ST[i]->S--;ST[LG]->C--;}
if(x==2){printf("%d\n",ST[LG]->C);continue;}
for(i=LG-1,j=LG;j;i--,j--)
{
if(ST[j]->C+ST[j]->S)break;
ST[i]->N[L[i]-'a']=0;X=ST[j];ST[j]=0;delete X;
}
if(x==3)printf("%d\n",j);
}
return 0;
}
void INIT()
{
for(Q=R,i=0;i<LG;i++)
{
ST[i]=Q;
if(!Q->N[L[i]-'a'])Q->N[L[i]-'a']=new trie;
Q=Q->N[L[i]-'a'];
}
ST[LG]=Q;
}