Cod sursa(job #3274682)

Utilizator BogdanBurescuBogdan Burescu BogdanBurescu Data 8 februarie 2025 09:52:36
Problema Trie Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.63 kb
#include <bits/stdc++.h>

using namespace std;

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

class Trie
{
    int cnt=0,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);
        }
    }

    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 1+next[str[pos]-'a']->lcp(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 main()
{
    Trie trie;
    int cer;
    string str;
    while(fin>>cer>>str)
    {
        if(cer==0)
            trie.insert(str);
        if(cer==1)
            trie.erase(str);
        if(cer==2)
            fout<<trie.count(str)<<'\n';
        if(cer==3)
            fout<<trie.lcp(str)<<'\n';
    }
    return 0;
}