Cod sursa(job #3225487)

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

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

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

char w[25];
void addw(), delw();
int cntw(), lmax();

int main()
{
    root = new Trie;
    int op;
    while (fin >> op >> w){
        if (op == 0) addw();
        else if (op == 1) delw();
        else if (op == 2) fout << cntw() << '\n';
        else fout << lmax() << '\n'; 
    }

    return 0;
}

void addw(){
    int i = 0;  
    Trie *aux = root;
    while (i < strlen(w)){
        int c = w[i] - 'a';
        if (aux->f[c] == NULL)
            aux->f[c] = new Trie;
        aux = aux->f[c];
        aux->cnt++; i++;
    }
    aux->cntf++;
}
void delw(){
    int i = 0;  
    Trie *aux = root;
    while (i < strlen(w)){
        int c = w[i] - 'a';
        aux = aux->f[c];
        aux->cnt--; i++;
    }
    aux->cntf--;
}
int cntw(){
    int i = 0;  
    Trie *aux = root;
    while (i < strlen(w)){
        int c = w[i] - 'a';
        if (aux->f[c] == NULL || aux->f[c]->cnt == 0) 
            return 0;
        aux = aux->f[c];
        i++;
    }
    return aux->cntf;
}
int lmax(){
    int i = 0;  
    Trie *aux = root;
    while (i < strlen(w)){
        int c = w[i] - 'a';
        if (aux->f[c] == NULL || aux->f[c]->cnt == 0)
            return i;
        aux = aux->f[c];
        i++;
    }
    return i;
}

//18:14