Cod sursa(job #1387105)

Utilizator robertstrecheStreche Robert robertstreche Data 13 martie 2015 18:09:43
Problema Trie Scor 55
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 kb
#include <fstream>
#include <cstring>

#define LMAX 21
#define NR_CHAR 26

using namespace std;

ifstream f("trie.in");
ofstream g("trie.out");

int tip;
char S[LMAX];

struct boss{
  int ap;
  boss *son[NR_CHAR];
  boss(){
   ap=0;
   memset(son,0,sizeof(son));
   }
}*rad,*nod,*aux;

inline void insert_trie()
{
    int poz=0;
    nod=rad;
    while (S[poz])
     {
        if (nod->son[S[poz]-'a']==NULL)
         {
             aux=new boss;
             aux->ap=0;
             nod->son[S[poz]-'a']=aux;
         }
        nod=nod->son[S[poz]-'a'];
        poz++;
     }
    nod->ap++;
}
inline void erase_trie()
{
    int poz=0;
    nod=rad;
    while (S[poz])
     {
        nod=nod->son[S[poz]-'a'];
        poz++;
     }
    nod->ap--;
}
inline void nr_ap()
{
    int poz=0;
    nod=rad;
    while (S[poz])
     {
         if (nod->son[S[poz]-'a']==NULL)break;
         nod=nod->son[S[poz]-'a'];
         poz++;
     }
    if (S[poz])
      g<<0<<'\n';
    else
      g<<nod->ap<<'\n';
}
inline void find_prefix()
{
     int poz=0;
     nod=rad;
     while (nod->son[S[poz]-'a'] && S[poz])
      {
         nod=nod->son[S[poz]-'a'];
         poz++;
      }
    g<<poz<<'\n';
}
int main()
{
   rad=new boss;
   rad->ap=0;
   while (f>>tip)
    {
        f>>S;
        switch (tip){
         case 0: {insert_trie();break;}
         case 1: {erase_trie();break;}
         case 2: {nr_ap();break;}
         case 3: {find_prefix();break;}
        }
    }
   f.close();
   g.close();
}