Pagini recente » Cod sursa (job #29447) | Cod sursa (job #1867240) | Cod sursa (job #1170871) | Cod sursa (job #280012) | Cod sursa (job #1945770)
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
struct trie{
int ap;
int nrc;
trie *copii[31];
trie()
{ap=0;nrc=0; for(int i=0;i<=30;i++)
copii[i]=0;
}
}*root;
void ins(trie *&root,char *p)
{
if(!*p)
{
root->ap++;
return ;
}
if(root->copii[*p-'a'])
{
ins(root->copii[*p-'a'],p+1);
return;
}
root->copii[*p-'a']=new trie;
ins(root->copii[*p-'a'],p+1);
}
void delete1(trie *&root,char *p)
{
if(!*p)
{
root->ap--;
return ;
}
delete1(root->copii[*p-'a'],p+1);
}
void tipar(trie *&root,char *p)
{
if(!*p)
{
printf("%d\n",root->ap);
return;
}
tipar(root->copii[*p-'a'],p+1);
}
void tipar2(trie *&root,char *p,int nr)
{
if(!*p||root->copii[*p-'0']==0)
{
printf("%d\n",nr);
return ;
}
tipar2(root->copii[*p-'a'],p+1,nr+1);
}
void citire()
{
int x;
root=new trie;
char s[200];
int y;
while(y>0)
{
y=scanf("%d ",&x);
scanf("%s",s);
char *p=s;
if(x==0)
ins(root,p);
else if(x==1)
delete1(root,p);
else
if(x==2)
tipar(root,p);
else
tipar2(root,p,0);
}
}
int main()
{
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
citire();
//cout << "Hello world!" << endl;
return 0;
}