Cod sursa(job #1156415)

Utilizator CypyNXEnculescu Ciprian CypyNX Data 27 martie 2014 17:25:59
Problema Trie Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.02 kb
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <fstream>

using namespace std;

ifstream in("intrare.txt");
ofstream out("iesire.txt");
/**_______________________________________________________________________________________________ Clasa Nod */
class Nod
{
    int APARITII;
    bool INFO;
    Nod *FII[26];
    public:
        Nod ();
    friend class Trie;
};

Nod::Nod()
{
    INFO=APARITII=0;
    memset(FII,0,sizeof(FII));
}
/**______________________________________________________________________________________________ Clasa Trie */
class Trie
{
    Nod *START;
    public:
        Trie();
        void insert(char c[]);
        int getAPARITII(char c[]);
        void stergere(char c[]);
};

Trie::Trie()
{
    START=new Nod();
}

void Trie::insert(char c[])
{
    Nod *v_curent=START;
    for(int i=0;i<strlen(c);i++)
        if( v_curent->FII[c[i]-'a'] ) v_curent=v_curent->FII[ c[i]-'a' ];
           else {
                 Nod *n;n=new Nod();
                 v_curent->FII[c[i]-'a']=n;
                 v_curent=n;
                }
    v_curent->INFO=1;
    v_curent->APARITII++;
}

int Trie::getAPARITII(char c[])
{
    int ok=1;
    Nod *v_curent=START;
    for(int i=0;i<strlen(c);i++)
       {
        if(v_curent->FII[c[i]-'a']) v_curent=v_curent->FII[ c[i]-'a' ];
          else {
                i=strlen(c);
                ok=0;
               }
       }
    if(v_curent->INFO && ok) {out<<v_curent->APARITII<<endl;return v_curent->APARITII;}
      else out<<"0"<<endl;
    return 0;
}


int main()
{
Trie dictionar;
char cuvant[20];
int n;
while(in.get()!=EOF)
    {
     in>>n;
     in>>cuvant;
     switch (n) {case  0  : {dictionar.insert(cuvant);
                             break;}
                 case  1  : {break;}
                 case  2  : {dictionar.getAPARITII(cuvant);
                             break;}
                 case  3  : {break;}
                 }
    }


    in.close();
    return 0;
}