Cod sursa(job #2399157)

Utilizator Daria09Florea Daria Daria09 Data 7 aprilie 2019 00:52:49
Problema Trie Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.56 kb
#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;
}