Cod sursa(job #1351886)

Utilizator victor_crivatCrivat Victor victor_crivat Data 21 februarie 2015 11:41:20
Problema Trie Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <fstream>
#define miel 10000
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
char s[miel];
struct trie
{ trie *fii[26];
    int nrapar;
    int nrfii;
    trie ()
    {nrapar=0;
     nrfii=0;
     for (int i=0; i<26; i++) fii[i]=NULL;
    }

};
void insert(trie *t,char *s)
{if (*s==NULL) {t->nrapar++; return;}
    if (t->fii[s[0]-'a']==NULL)
    {
        t->fii[s[0]-'a']=new trie;
        t->nrfii++;
    }
    insert (t->fii[s[0]-'a'],s+1) ;
}
void remove(trie *t,char *s)
{if (*s==NULL) {t->nrapar--; return;}
     if (t->fii[s[0]-'a']->nrapar==0 && t->fii[s[0]-'a']->nrfii==0)
    {
        t->nrfii--;
        t->fii[s[0]-'a']=NULL;
    }
}
int number (trie *t,char *s)
{
    if (t==NULL) return 0;
    if (*s==NULL)
    {
        return t->nrapar;
    }
    number(t->fii[s[0]-'a'],s+1);
}
int prefix (trie *t,char *s,int k)
{
    if (t->fii[s[0]-'a']==NULL||*s==NULL)
    {
        return k;
    }
    else
        return prefix(t->fii[s[0]-'a'],s+1,k+1);

}
int main()
{trie *t=new trie; int x; char space;
    while (!f.eof())
{f>>x;
f>>space;
    f.get(s,miel);
f.get();
if (*s==NULL) continue;
    if (x==0) insert(t,s);
    if (x==1) remove(t,s);
    if (x==2) g<<number(t,s)<<'\n';
    if (x==3) g<<prefix(t,s,0)<<'\n';
}
}