Cod sursa(job #2404670)

Utilizator darisavuSavu Daria darisavu Data 13 aprilie 2019 11:27:18
Problema Trie Scor 5
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.42 kb
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
int n,k,op,nod;
struct Tnod
{
    char chr;
    int nrcuv;
    int nrap;
    int urm;
};
char w[30];
vector<vector<Tnod> >trie;
void ad()
{
    int nod=0,j=0,n=strlen(w),i;
    while(j<n)
    {
        int sz=trie[nod].size();
        bool ok=false;
        for( i=0;i<sz;i++)
        {
            if(trie[nod][i].chr==w[j]) {ok=true;break;}
        }
        if(ok==1)
        {
            if(j==n-1) trie[nod][i].nrcuv++;
            trie[nod][i].nrap++;
            ++j;
            nod=trie[nod][i].urm;
        }
        else
        {
            Tnod aux;
            aux.chr=w[j];
            aux.nrcuv=0;
            aux.nrap=1;
            aux.urm=++k;
            if(j==n-1) aux.nrcuv=1;
            trie.push_back(vector<Tnod>());
            trie[nod].push_back(aux);
            nod=k;
            j++;
        }
    }
}
void del()
{
    int nod=0,j=0,n=strlen(w),i;
    bool ok=false;
    while(j<n)
    {
        int sz=trie[nod].size();
        for(i=0;i<sz;i++)
        {
            if(trie[nod][i].chr==w[j]) {ok=true;break;}
        }
            if(j==n-1) trie[nod][i].nrcuv--;
            trie[nod][i].nrap--;
            ++j;
            nod=trie[nod][i].urm;
    }
}
void nrap()
{
     int nod=0,j=0,n=strlen(w),i;
    bool ok=false;
    while(j<n-1)
    {
        int sz=trie[nod].size();
        for( i=0;i<sz;i++)
        {
            if(trie[nod][i].chr==w[j]) {ok=true;break;}
        }
            ++j;
            nod=trie[nod][i].urm;
    }
    g<<trie[nod][i].nrcuv<<'\n';
}
void prefix()
{
     int nod=0,j=0,n=strlen(w),i,p;
    while(j<n)
    {
        int sz=trie[nod].size();
        bool ok=false;
        for( i=0;i<sz;i++)
        {
            if(trie[nod][i].chr==w[j]&&trie[nod][i].nrap!=0) {ok=true;break;}
        }
        if(ok==1)
        {
            ++j;
            nod=trie[nod][i].urm;
        }
        else
        {
           g<<j<<'\n';
           break;
        }
    }
}
int main()
{
    trie.push_back(vector<Tnod>());
    while(f>>op)
    {
        f>>w;
        switch(op)
        {
        case 0: ad();break;
        case 1: del();break;
        case 2: nrap();break;
        case 3: prefix();break;
        default: break;
        }
    }
    return 0;
}