Cod sursa(job #2706586)

Utilizator mitocaru_marioMitocaru Mario-Alexandru mitocaru_mario Data 15 februarie 2021 13:46:21
Problema Trie Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.85 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
struct nod
{
    int ap,pass;
    nod *F[26];
    nod()
    {
        ap=0;
        pass=0;
        for(int i=0;i<26;i++)
            F[i]=NULL;
    }
};
nod *root;
int tip;
char w[22];
int main()
{
    root = new nod;
    while(f>>tip)
    {
        f>>w;
        if(tip==0)
        {
            /// adaugare cuvant
            nod *it;
            char *p;
            for(p=w,it=root;*p;p++)
            {
                int i=*p-'a';
                if(!it->F[i])
                    it->F[i]=new nod;
                it=it->F[i];it->pass++;
            }
            it->ap++;
        }
        if(tip==1)
        {
            /// stergere cuvant
            nod *it;
            char *p;
            for(p=w,it=root;*p;p++)
            {
                int i=*p-'a';
                it=it->F[i];
                it->pass--;
            }
            it->ap--;
        }
        if(tip==2)
        {
            /// numar aparitii cuvant
            nod *it;
            char *p;
            for(p=w,it=root;*p;p++)
            {
                int i=*p-'a';
                if(it->F[i]==NULL)break;
                it=it->F[i];
                if(it->pass==0)break;
            }
            if(*p)
                g<<"0\n";
            else
                g<<it->ap<<'\n';
        }
        if(tip==3)
        {
            /// prefix maxim
            int lung = 0;
            nod *it;
            char *p;
            for(p=w,it=root;*p;p++)
            {
                int i=*p-'a';
                if(it->F[i]==NULL)break;
                it=it->F[i];
                if(it->pass==0)break;
                lung++;
            }
            g<<lung<<'\n';
        }

    }

    return 0;
}