Cod sursa(job #2436918)

Utilizator stefantagaTaga Stefan stefantaga Data 7 iulie 2019 17:04:05
Problema Trie Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.57 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
int tip,n,j;
char s[100];
struct trie
{
    int nr,nrcuv;
    vector <int> v;
    trie()
    {
        nr=nrcuv=0;
        v.resize(26);
    }
};
vector <trie> arb;
void update()
{
    int nod=0;
    n=strlen(s);
    for (j=0;j<n;j++)
    {
        if (arb[nod].v[s[j]-'a']==0)
        {
            arb.push_back(trie());
            arb[nod].v[s[j]-'a']=arb.size()-1;
        }
        nod=arb[nod].v[s[j]-'a'];
        arb[nod].nr++;
    }
    arb[nod].nrcuv++;
}
void sterge()
{
    int nod=0;
    n=strlen(s);
    for (j=0;j<n;j++)
    {
        nod=arb[nod].v[s[j]-'a'];
        arb[nod].nr--;
    }
    arb[nod].nrcuv--;
}
int nrcuv()
{
    int nod=0;
    n=strlen(s);
    for (j=0;j<n;j++)
    {
        nod=arb[nod].v[s[j]-'a'];
        if (arb[nod].nr==0)return 0;
    }
    return arb[nod].nrcuv;
}
int celmailungprefix()
{
    int nod=0;
    n=strlen(s);
    for (j=0;j<n;j++)
    {
        nod=arb[nod].v[s[j]-'a'];
        if (arb[nod].nr==0)
        {
            return j;
        }
    }
    return n;
}
int main()
{
int     nod=0;
    arb.push_back(trie());
    while (f>>tip>>s)
    {
        if (tip==0)
        {
            update();
        }
        else
        if (tip==1)
        {
            sterge();
        }
        else
        if (tip==2)
        {
            g<<nrcuv()<<'\n';
        }
        else
        {
            g<<celmailungprefix()<<'\n';
        }
    }
    return 0;
}