Pagini recente » Cod sursa (job #586315) | Cod sursa (job #125487) | Cod sursa (job #156010) | Cod sursa (job #2264485) | Cod sursa (job #780733)
Cod sursa(job #780733)
#include <fstream>
#include <string.h>
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
struct nod{
short int cuv[26];
short int inf;
nod *p[26];};
nod *rad;
int tip,lg,i,aux;
char s[25],c;
void adauga(short int h,nod *x);
void sterge(short int h,nod *x);
int nrap(short int h,nod *x);
int prefix(short int h,nod *x);
int main(){
rad=new nod;
rad->inf=0;
for(i=0;i<=25;i++)
rad->cuv[i]=0;
while(1){
f>>tip;
f.get(c);
f.getline(s,23,'\n');
if(f.eof())
break;
lg=strlen(s);
switch(tip){
case 0:
adauga(0,rad);
break;
case 1:
sterge(0,rad);
break;
case 2:
g<<nrap(0,rad)<<'\n';
break;
default:
g<<prefix(0,rad)<<'\n';}}
f.close();
g.close();
return 0;
}
void adauga(short int h,nod *x){
if(h==lg)
x->inf++;
else{
aux=s[h]-'a';
if(!x->cuv[aux]){
x->p[aux]=new nod;
x->p[aux]->inf=0;
for(i=0;i<=25;i++)
x->p[aux]->cuv[i]=0;}
x->cuv[s[h]-'a']++;
adauga(h+1,x->p[aux]);}}
void sterge(short int h,nod *x){
if(h==lg)
x->inf--;
else{
x->cuv[s[h]-'a']--;
sterge(h+1,x->p[s[h]-'a']);
if(!(x->cuv[s[h]-'a']))
delete x->p[s[h]-'a'];}}
int nrap(short int h,nod *x){
if(h==lg)
return x->inf;
else{
if(!(x->cuv[s[h]-'a']))
return 0;
else
return nrap(h+1,x->p[s[h]-'a']);}}
int prefix(short int h,nod *x){
if(!(x->cuv[s[h]-'a'])||h==lg)
return h;
else
return prefix(h+1,x->p[s[h]-'a']);}