Pagini recente » Cod sursa (job #2030847) | Cod sursa (job #2753296) | Cod sursa (job #1243162) | Cod sursa (job #2623868) | Cod sursa (job #1290671)
#include <fstream>
using namespace std;
ifstream fin("trie.in");
ofstream fout("trie.out");
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()
{
R=new trie;
for(;fin>>a;)
{
fin>>w;
if(a==0)
{
for(P=R,p=w;*p;p++)
{
if(!P->s[*p-'a'])
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'];
}
if(*p)
fout<<"0\n";
else
fout<<P->v<<"\n";
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++;
}
fout<<k<<"\n";
}
return 0;
}