Cod sursa(job #1387086)

Utilizator robertstrecheStreche Robert robertstreche Data 13 martie 2015 18:00:48
Problema Trie Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.53 kb
#include <fstream>

#define LMAX 30
#define NR_CHAR 30

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(){
  for (int i=0;i<=NR_CHAR;i++)
   son[i]=0;}
}*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();
}