Pagini recente » Cod sursa (job #1665200) | Cod sursa (job #1518804) | Cod sursa (job #2789126) | Cod sursa (job #1731059) | Cod sursa (job #1053248)
#include<cstdio>
#include<cstring>
using namespace std;
int Key,N,Letter;
char Word[25];
struct Trie
{
int Count,Was;
Trie *Son[30];
Trie()
{
Count=Was=0;
memset(Son,0,sizeof(Son));
}
};
Trie *Root=new Trie;
void Add(Trie *T,int Poz)
{
if(Poz==N) {T->Count++; return;}
Letter=Word[Poz]-'a';
if(!T->Son[Letter]) T->Son[Letter]=new Trie;
T->Son[Letter]->Was++;
Add(T->Son[Letter],Poz+1);
}
void Delete(Trie *T,int Poz)
{
if(Poz==N) {T->Count--; return;}
Letter=Word[Poz]-'a';
T->Son[Letter]->Was--;
Delete(T->Son[Letter],Poz+1);
}
void Count(Trie *T,int Poz)
{
if(Poz==N) {printf("%d\n",T->Count); return;}
Letter=Word[Poz]-'a';
if(!T->Son[Letter] || !T->Son[Letter]->Was) {printf("0\n"); return;}
Count(T->Son[Letter],Poz+1);
}
void Prefix(Trie *T,int Poz)
{
if(Poz==N) {printf("%d\n",N); return;}
Letter=Word[Poz]-'a';
if(!T->Son[Letter] || !T->Son[Letter]->Was) {printf("%d\n",Poz); return;}
Prefix(T->Son[Letter],Poz+1);
}
int main()
{
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
while(scanf("%d %s",&Key,Word)+1)
{
N=strlen(Word);
if(Key==0) Add(Root,0);
else if(Key==1) Delete(Root,0);
else if(Key==2) Count(Root,0);
else Prefix(Root,0);
}
return 0;
}