Cod sursa(job #3152238)

Utilizator MerlinTheWizardMelvin Abibula MerlinTheWizard Data 24 septembrie 2023 14:09:12
Problema Trie Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.41 kb
#include<iostream>
#include<stdio.h>

using namespace std;

struct trie
{
    int nr, rasp;
    trie *fii[27];
};
trie* init = new trie();

int max1 = 0;

void op0(trie *t, char *c)
{
    t -> nr++;
    if(*c == '\0')
    {
        t -> rasp++;
        return;
    }
    if(t -> fii[*c - 'a'] == NULL)
        t -> fii[*c - 'a'] = new trie();
    op0(t -> fii[*c - 'a'], c + 1);
}

void op1(trie *t, char *c)
{
    t -> nr--;
    if(*c == '\0')
    {
        t -> rasp--;
        return;
    }

    op1(t -> fii[*c - 'a'], c + 1);
}

void op2(trie *t, char *c)
{
    if(*c == '\0')
    {
        cout << t -> rasp << "\n";
        return;
    }

    if(t -> fii[*c - 'a'] == NULL)
    {
        cout << "0\n";
        return;
    }

    op2(t -> fii[*c - 'a'], c + 1);
}

void op3(trie *t, char *c)
{
    if(*c == '\0' || t -> fii[*c - 'a'] == NULL || !(t -> fii[*c - 'a'] -> nr))
        return;
    max1++;
    op3(t -> fii[*c - 'a'], c + 1);
}

int main()
{
    freopen("trie.in", "r", stdin);
    freopen("trie.out", "w", stdout);

    int x;
    char y[27];
    while(cin >> x >> y)
    {
        if(x == 0)
            op0(init, y);
        else if(x == 1)
            op1(init, y);
        else if(x == 2)
            op2(init, y);
        else
        {
            max1 = 0;
            op3(init, y);
            cout << max1 << "\n";
        }
    }
}