Pagini recente » Cod sursa (job #504168) | Istoria paginii runda/runda_ezoterica_5 | Cod sursa (job #1156415)
#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;
}