Cod sursa(job #2750264)

Utilizator razvan1403razvan razvan1403 Data 10 mai 2021 14:13:49
Problema Trie Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 6.34 kb
#include<bits/stdc++.h>
#include<queue>
#include<stack>
#define INFILE fin("trie.in");
#define OUTFILE fout("trie.out");

using namespace std;

ifstream INFILE
ofstream OUTFILE

int Admitere_Bucuresti_2010_a(int a)
{
    int contor2,contor3,contor5;
    contor2 = contor3 = contor5 = 0;
    while(a%2==0)
    {
        contor2++;
        a=a/2;
    }
    while(a%3==0)
    {
        contor3++;
        a=a/3;
    }
    while(a%5==0)
    {
        contor5++;
        a=a/5;
    }
    return(a==1)?1:0;
}

void Admitere_Bucuresti_2010_b()
{
    unsigned int n;
    cin>>n;
    int i,contor = 0;
    i = 1;
    while(contor<n)
    {
        if(Admitere_Bucuresti_2010_a(i) == 1)
        {
            contor++;
            cout<<i<<" ";
        }
        i++;
    }
}

long produs_maxim,n,k;
int stiva[101];

void Afisare_Admitere_Bucuresti_2011_a(int k)
{
    int i;
    long produs = 1;
    for(i=1;i<=k;i++)
    {
        produs = produs * stiva[i];
    }
    if(produs > produs_maxim)
    {
        produs_maxim = produs;
    }

}

void Admitere_Bucuresti_2011_a(int i,int k)
{
    int j;
    if(k==n)
        Afisare_Admitere_Bucuresti_2011_a(i-1);
    else{
        for(j=1;j<=n-k;j++)
        {
            if(j>=stiva[i-1])
            {
                stiva[i] = j;
                Admitere_Bucuresti_2011_a(i+1,k+j);
            }
        }
    }
}

long RidicarePutereLogaritmica(long n,long p)
{
    long r = 1;
    while(p)
    {
        if(p%2==1)
            r=r*n;
        p=p/2;
        n=n*n;
    }
    return r;
}

void Admitere_Bucuresti_2011_b()
{
    long suma1 = 0;
    suma1 = RidicarePutereLogaritmica(3,(n-1)/3);
    long suma2 = 0;
    if(n%3==2)
    {
        suma2 = RidicarePutereLogaritmica(3,(n-1)/3)-1 + RidicarePutereLogaritmica(3,(n-1)/3);
    }
    else suma2 = RidicarePutereLogaritmica(3,n/3)-1;
    produs_maxim = suma1 + suma2;
    cout<<produs_maxim;
}

void Admitere_Bucuresti_2012_a()
{
    int x = 1;
    while((x*(x+1)/2<n))
    {
        x++;
    }
    int i,j;
    for(i=1;i<=x;i++)
    {
        for(j=1;j<=i;j++)
        {
            if(n!=0)
            {
                cout<<i<<" ";
                n--;
            }
        }
    }
}

void Admitere_Bucuresti_2012_b()
{
    int x =1;
    while(x*(x+1)/2 < n)
    {
        x++;
    }
    cout<<x;
}
void Admitere_Bucuresti_2012_c()
{
    int y[1001];
    int i;
    int frv[100] = {};
    int ok = 1;
    int x =1;
    while(x*(x+1)/2 < n)
    {
        x++;
    }
    for(i=1;i<=n;i++)
    {
        cin>>y[i];
        if(y[i] > x)
            ok=0;
        else frv[y[i]]++;
    }
    
    for(i=1;i<=x && ok == 1;i++)
    {
        if(frv[i]!=i)
            ok = 0;
    }

    if(frv[x] > x)
    {
        ok=0;
    }
    if(ok==0)
        cout<<"NU";
    else cout<<"DA";
}

void Admitere_Bucuresti_2015_a()
{
    unsigned int a,b,c,n,i,j;
    int s[1001];
    cin>>a>>b>>c>>n;
    for(i=1;i<=n;i++)
    {
        cin>>s[i];
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(s[i]!=s[j] && a*s[i]*s[i] + b*s[j]*s[j] == c)
            {
                cout<<"("<<s[i]<<","<<s[j]<<")"<<" ";
            }
        }
    }
    cout<<(char)(8);
}

void Admitere_Bucuresti_2018_a()
{
    unsigned L;
    char sir[10001];
    cin>>L;
    cin.get();
    cin.getline(sir,10000);
    int i, j, last_position = -1, n = strlen(sir), contor;
    for(i = 0; i < n; i++)
    {
        contor = 0;
        j = i;
        last_position = -1;
        while(contor <= L && j < n)
        {
            j++;
            contor++;
            if(sir[j] == ' '|| j == n-1)
                last_position = j;
        }
        j = last_position;
        for(int k=i;k<=j;k++)
        {
            cout<<sir[k];
        }
        cout<<'\n';
        i = j;
    }
}

void Admitere_Bucuresti_2018_b()
{
    unsigned N,L;
    char sir[1001][1001];
    cin>>L>>N;
    cin.get();
    for(int i=1;i<=N;i++)
    {
        cin.getline(sir[i],1000);
    }
    int ok = 0;
    for(int i=1;i<N && ok == 0;i++)
    {
        for(int j=0;j<strlen(sir[i]);i++)
        {
            if(sir[i][j] == ' ')
            {
                if(j == 0)
            {
                if(sir[i+1][j] == ' ' || sir[i+1][j+1] == ' ')
                    ok = 1;
            }
            else if(strlen(sir[i]) <= strlen(sir[i+1]) && j == strlen(sir[i])-1)
                {
                    if(sir[i+1][j] == ' ' || sir[i+1][j-1] == ' ')
                        ok = 1;
                }
            else{
                if(sir[i+1][j] == ' ' || sir[i+1][j+1] == ' ' || sir[i+1][j-1])
                    ok = 1;
            }
            }
        }
    }
    if(ok == 1)
        cout<<"DA";
    else cout<<"NU";
}

class Trie{
    private:
        int cnt = 0;
        int lvs = 0;
        Trie *next[26] = {};
    public:
        void insert(const string& str, int pos = 0)
        {
            lvs++;
            if(pos == (int)str.size())
            {
                cnt++;
            }
            else{
                if(!next[str[pos] - 'a'])
                {
                    next[str[pos] - 'a'] = new Trie;
                }
                next[str[pos] - 'a']->insert(str,pos+1);
            }
        }

        void erase(const string& str,int pos = 0)
        {
            lvs--;
            if(pos==(int)str.size())
                cnt--;
            else{
                next[str[pos] - 'a']->erase(str,pos+1);
                if(!next[str[pos] - 'a'] -> lvs)
                {
                    delete next[str[pos] - 'a'];
                    next[str[pos] - 'a'] = nullptr;
                }
            }
        }

        int count(const string& str, int pos = 0)
        {
            if(pos == (int)str.size())
            {
                return cnt;
            }
            if(!next[str[pos] - 'a'])
                return 0;
            return next[str[pos] - 'a']->count(str,pos+1);
        }

        int lcp(const string& str,int pos = 0)
        {
            if(pos == (int)str.size())
                return 0;
            if(!next[str[pos] - 'a'])
                return 0;
            return next[str[pos] - 'a']->lcp(str,pos+1);
        }
};

int main()
{
	Trie tr;
    int t;
    string sir;
    while(fin>>t>>sir)
    {
        if(!t)
            tr.insert(sir);
        else if(t == 1)
            tr.erase(sir);
        else if(t == 2)
            fout<<tr.count(sir)<<'\n';
        else fout<<tr.lcp(sir)<<'\n';
    }
	return 0;
}