Cod sursa(job #3212418)

Utilizator maiaauUngureanu Maia maiaau Data 11 martie 2024 18:25:52
Problema Trie Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.75 kb
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
#define pb push_back

ifstream fin("trie.in");
ofstream fout("trie.out");

const int N = 2e4+3;

struct nod {
    int cnt, cntf;
    nod* f[26];
    nod(){
        cnt = cntf = 0;
        for (int i = 0; i < 26; i++)
            f[i] = NULL;
    }
} *root;

int t;
char w[23];

int main()
{
    root = new nod;
    while (fin >> t >> w){
        if (!t){
            nod *aux = root;
            char *p = w;
            while (*p != '\0'){
                int c = *p - 'a';
                if (aux->f[c] == NULL)
                    aux->f[c] = new nod;
                aux = aux->f[c]; 
                aux->cnt++; p++;
            }
            aux->cntf++;
        }
        else if (t == 1){
            nod *aux = root;
            char *p = w;
            while (*p != '\0'){
                int c = *p - 'a';
                aux = aux->f[c]; 
                aux->cnt--; p++;
            }
            aux->cntf--;
        }
        else if (t == 2){
            nod *aux = root;
            char *p = w;
            while (*p != '\0'){
                int c = *p - 'a';
                if (aux->f[c] == NULL){
                    fout << 0 << '\n';
                    break;
                }
                aux = aux->f[c]; p++;
            }
            if (*p == '\0') fout << aux->cntf << '\n';
        }
        else {
            nod *aux = root;
            char *p = w; int l = 0;
            while (*p != '\0'){
                int c = *p - 'a';
                if (aux->f[c] == NULL || !aux->f[c]->cnt){
                    break;
                }
                aux = aux->f[c]; p++; l++;
            }
            fout << l << '\n';
        }
    }
    
    return 0;
}