Pagini recente » Cod sursa (job #984878) | Cod sursa (job #2251384) | Cod sursa (job #2573677) | Cod sursa (job #1105571) | Cod sursa (job #2399157)
#include <bits/stdc++.h>
#define maxword 25
#define sigma 26
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
struct Trie
{
Trie *son[sigma];
int wordcount,endingcount;
Trie()
{
this->wordcount=this->endingcount=0;
for(int i=0; i<sigma; ++i)
this->son[i]=NULL;
}
};
Trie *root=new Trie;
void Insert(Trie *node,char *word)
{
node->wordcount++;
if(*word==NULL)
{
node->endingcount++;
return;
}
if(node->son[*word-'a']==NULL)
node->son[*word-'a']=new Trie;
Insert(node->son[*word-'a'],word+1);
}
void Erase(Trie *node,char *word)
{
node->wordcount--;
if(*word==NULL)
{
node->endingcount--;
return;
}
Erase(node->son[*word-'a'],word+1);
}
int CountApparition(Trie *node,char *word)
{
if(*word==NULL)
return node->endingcount;
if(node->son[*word-'a']==NULL)
return 0;
return CountApparition(node->son[*word-'a'],word+1);
}
int Prefix(Trie *node,char *word)
{
if(node->son[*word-'a']==NULL||*word==NULL||node->son[*word-'a']->wordcount==0)
return 0;
return Prefix(node->son[*word-'a'],word+1)+1;
}
int main()
{
char word[maxword];
int type;
while(f>>type&&f>>word)
{
if(type==0)
Insert(root,word);
else if(type==1)
Erase(root,word);
else if(type==2)
g<<CountApparition(root,word)<<'\n';
else
g<<Prefix(root,word)<<'\n';
}
return 0;
}