Cod sursa(job #975253)

Utilizator Daniel3717Aleca Daniel Adrian Daniel3717 Data 19 iulie 2013 16:01:20
Problema Trie Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.51 kb
#include <iostream>
#include <fstream>
using namespace std;
struct nod{
int inf;
nod *c[30],*ant;
} *rad,*p,*q;
int i,t,k,n;
char s[30];
char ch;
int main(void)
{
    FILE * f;
    f=fopen("trie.in","r");
    ofstream g("trie.out");
    rad=new(nod);
    for (i=0;i<26;i++)
        rad->c[i]=NULL;
    rad->inf=0;
    rad->ant=NULL;
    while (!feof(f))
    {
        fscanf(f,"%d",&t);
        fscanf(f,"%ch",&ch);
        fgets(s,22,f);
        if (!feof(f))
        {if (t==0)
        {
            p=rad;
            n=0;
            while (s[n]!=10)
            {
                if (p->c[(s[n]-'a')]!=NULL)
                    {
                        p=p->c[s[n]-'a'];
                    }
                else
                {
                    q=new(nod);
                    for (i=0;i<26;i++)
                        q->c[i]=NULL;
                    q->inf=0;
                    p->c[s[n]-'a']=q;
                    q->ant=p;
                    (p->inf)++;
                    p=q;
                }
                n++;
            }
            p->inf=p->inf+100;
            rad->inf=0;
        }
        if (t==1)
        {
            p=rad;
            n=0;
            while (s[n]!=10)
            {
                p=p->c[(s[n]-'a')];
                n++;
            }
            if (p->inf>=200)
                p->inf=p->inf-100;
            else
                if ((p->inf)%100>0)
                    p->inf=p->inf-100;
                else
                {
                    q=p;
                    p=p->ant;
                    p->c[s[n-1]-'a']=NULL;
                    n--;
                    delete(q);
                    while (p->inf==1)
                    {
                        q=p;
                        p=p->ant;
                        p->c[s[n-1]-'a']=NULL;
                        n--;
                        delete(q);
                    }
                }

        }
        if (t==2)
        {
            p=rad;
            n=0;
            while (s[n]!=10)
            {
                p=p->c[(s[n]-'a')];
                n++;
            }
            g<<(p->inf)/100<<'\n';
        }
        if (t==3)
        {
            p=rad;
            k=-1;
            n=0;
            while ((s[n]!=10)&&(p!=NULL))
            {
                p=p->c[(s[n]-'a')];
                n++;
                k++;
            }
            g<<k<<'\n';
        }}
    }
    g.close();
    return 0;
}