Cod sursa(job #3311104)

Utilizator Dia3141Costea Diana Stefania Dia3141 Data 19 septembrie 2025 16:09:41
Problema Trie Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <fstream>
#define nmax (int)(1e5+1)
using namespace std;
ifstream cin("trie.in");
ofstream cout("trie.out");
int task,p,k,l;
char s[22];
struct nod{
    int fr[27],cuv,pref;
}t[300001];
void add(int nod){
    t[nod].pref++;
    if(s[p]==0){
        t[nod].cuv++;
        return ;
    }
    if(t[nod].fr[s[p]-'a']==0)
        t[nod].fr[s[p]-'a']=++k;
    add(t[nod].fr[s[p++]-'a']);
}
void cut(int nod){
    t[nod].pref--;
    if(s[p]==0){
        t[nod].cuv--;
        return ;
    }
    cut(t[nod].fr[s[p++]-'a']);
}
int ap(int nod){
    if(s[p]==0)
        return t[nod].cuv;
    if(t[nod].fr[s[p]-'a']!=0)
        return ap(t[nod].fr[s[p++]-'a']);
    return 0;
}
void lg(int nod){
    if(t[nod].pref<=0)
        return ;
    l=p;
    if(s[p]==0)
        return ;
    if(t[nod].fr[s[p]-'a']!=0){
        lg(t[nod].fr[s[p++]-'a']);
    }
}
int main()
{
    while(cin>>task>>s){
        p=0;
        if(task==0)
            add(0);
        else if(task==1)
            cut(0);
        else if(task==2)
            cout<<ap(0)<<'\n';
        else{
            l=0;
            lg(0);
            cout<<l<<'\n';
        }
    }
    return 0;
}