Cod sursa(job #1387148)

Utilizator robertstrecheStreche Robert robertstreche Data 13 martie 2015 19:21:34
Problema Trie Scor 55
Compilator cpp Status done
Runda Arhiva educationala Marime 1.48 kb
#include <fstream>
#include <cstring>

#define LMAX 31
#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(boss *nod,int poz)
{
    if (S[poz]==NULL)
     {
         nod->ap++;
         return;
     }
    if (nod->son[S[poz]-'a']==0)
     nod->son[S[poz]-'a']=new boss;

    insert_trie(nod->son[S[poz]-'a'],poz+1);
}
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(rad,0);break;}
         case 1: {erase_trie();break;}
         case 2: {nr_ap();break;}
         case 3: {find_prefix();break;}
        }
    }
   f.close();
   g.close();
}