Pagini recente » Cod sursa (job #140499) | Cod sursa (job #237682) | Cod sursa (job #317936) | Cod sursa (job #569797) | Cod sursa (job #2091997)
#include <fstream>
#include<cstring>
using namespace std;
ifstream cin ("trie.in");
ofstream cout ("trie.out");
char s[20];
struct trie
{
int nr,sol;
trie *fiu[26];
trie()
{
nr=0; sol=0;
for(int i=0;i<=25;i++) fiu[i]=0;
}
} *root=new trie;
void add (trie *&that,int poz)
{
if(poz==strlen(s)) {that->sol++; return;}
if(that->fiu[s[poz]-'a']==0) that->fiu[s[poz]-'a']=new trie,that->nr++;
add(that->fiu[s[poz]-'a'],poz+1);
}
void sterge (trie *&that,int poz)
{
if(poz==strlen(s)) {that->sol--;
if(that->sol==0 && that->nr==0 && that!=root)
delete that,that=NULL;
return;}
sterge(that->fiu[s[poz]-'a'],poz+1);
if(that->fiu[s[poz]-'a']==0) that->nr--;
if(that->nr==0 && that!=root && that->sol==0)
delete that,that=NULL;
}
void tipar (trie *&that,int poz)
{
if(poz==strlen(s)) {cout<<that->sol<<"\n"; return;}
if(that->fiu[s[poz]-'a']==0) {cout<<0<<"\n"; return;}
else tipar(that->fiu[s[poz]-'a'],poz+1);
}
void last (trie *&that,int poz)
{
if(that->fiu[s[poz]-'a']==0 || poz==strlen(s)) cout<<poz<<"\n";
else last(that->fiu[s[poz]-'a'],poz+1);
}
void da ()
{
}
void read ()
{ int x,k=0;
while(cin>>x)
{ k++;
if(k==4785) da();
cin>>s;
if(x==0) add(root,0);
else if(x==1) sterge(root,0);
else if(x==2) tipar(root,0);
else if(x==3) last(root,0);
}
}
int main()
{
read();
cin.close();
cout.close();
return 0;
}