Cod sursa(job #2651533)

Utilizator AACthAirinei Andrei Cristian AACth Data 22 septembrie 2020 20:39:51
Problema Trie Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.02 kb
#include <bits/stdc++.h>
#define Nmax 100005
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
vector < int > v[Nmax];
vector < int > copilas[Nmax];
int plod[Nmax];
struct nod
{
    char litera;
    int cate;
};
nod a[Nmax];
char word[21];
int new_node=1;
void check(int start)
{
    //  cout<<a[start].litera<<'\n';
    for(auto nod : v[start])
    {
        g<<start<< a[start].litera<<'/'<<a[start].cate<<' '<<nod<<a[nod].litera<<'/'<<a[nod].cate<<'\n';
        check(nod);
    }

}
void update_plozi_plus(int nod)
{
    plod[nod]++;
    if(!copilas[nod].empty())
        update_plozi_plus(copilas[nod][0]);
}
void update_plozi_minus(int nod)
{
    plod[nod]--;
    if(!copilas[nod].empty())
        update_plozi_minus(copilas[nod][0]);
}
void do0()
{
    int where = 0;
    int i;
    for(i=0; word[i]; i++)
    {
        bool gata = 0;

        bool found = 0;
        char litera = word[i];
        for(auto candidat : v[where])
        {
            if(a[candidat].litera == litera)
            {
                where = candidat;
                found = 1;
                if(!word[i+1])
                {
                    a[where].cate++;
                    update_plozi_plus(where);
                }
                break;
            }

        }
        if(!found)
        {
            //cout  << "2 10";
            for(; word[i]; i++)
            {
                v[where].push_back(new_node);
                copilas[new_node].push_back(where);
                a[new_node].litera =  word [i];
                where=new_node;
                new_node++;
            }
            a[where].cate++;
            update_plozi_plus(where);
            break;
        }
    }

    //g<<"||||||||||||||||||||||||||||||||||||||||||||||\n";
}
void do1()
{
    int where = 0;
    int i;
    for(i=0; word[i]; i++)
    {
        bool gata = 0;

        bool found = 0;
        char litera = word[i];
        for(auto candidat : v[where])
        {
            if(a[candidat].litera == litera)
            {
                where = candidat;
                found = 1;
                if(!word[i+1])
                {
                     a[where].cate--;
                     update_plozi_minus(where);
                }
                break;
            }
        }

    }

}
void do2()
{
    int where = 0;
    int i;
    for(i=0; word[i]; i++)
    {
        bool gata = 0;

        bool found = 0;
        char litera = word[i];
        for(auto candidat : v[where])
        {
            if(a[candidat].litera == litera)
            {
                where = candidat;
                found = 1;
                if(!word[i+1])
                {
                     g<< a[where].cate << '\n';
                     return;
                }

                break;
            }
        }
        if(!found)
        {
            g<<"0\n";
            return;
        }


    }
}
void do3()
{
    int where = 0;
    int i;
    int answer=0;
    for(i=0; word[i]; i++)
    {
        bool gata = 0;

        bool found = 0;
        char litera = word[i];
        for(auto candidat : v[where])
        {
            if(a[candidat].litera == litera)
            {
                where = candidat;
                found = 1;
                if(plod[where])answer = i+1;
                if(!word[i+1])
                {
                    g<<answer<< '\n';
                }
                break;
            }
        }
        if(!found)
        {

            g<<answer <<'\n';
            return;
        }

    }
}

int main(int start)
{
    a[0].litera = '*';
    int task;
    while(f>>task)
    {
        f>>word;
        if(task==0)
            do0();
        if(task==1)
            do1();
            if(task==2)
                 do2();
        if(task==3)
            do3();
        for(int i=0; i<=20; i++)
            word[i]='\0';
    }
    // check(0);
    return 0;
}