Cod sursa(job #3327071)

Utilizator AlexRadu010Radu Alexandru Timotei AlexRadu010 Data 2 decembrie 2025 10:14:34
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.6 kb
#include <bits/stdc++.h>

using namespace std;

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

const int lg=26;

struct trienode
{
    struct trienode *children[lg];
    int wordcnt;
    trienode()
    {
        wordcnt=0;
        for (int i=0; i<lg; i++)
            children[i]=NULL;
    }
};

trienode *root;

void insertword (trienode *root, string word)
{
    trienode *curr=root;
    for (auto c:word)
    {
        if (curr->children[c-'a']==NULL)
        {
            trienode *node=new trienode;
            curr->children[c-'a']=node;
        }
        curr=curr->children[c-'a'];
    }
    curr->wordcnt++;
}

trienode *deleteword (trienode *curr, string word, int k)
{
    if (!curr)
        return NULL;
    if (k==word.size())
    {
        if ()
    }
}

int frecv (trienode *root, string word)
{
    trienode *curr=root;
    for (auto c:word)
    {
        if (curr->children[c-'a']==NULL)
            return 0;
        curr=curr->children[c-'a'];
    }
    return curr->wordcnt;
}

int pref (trienode *root, string word)
{
    trienode *curr=root;
    int rez=0;
    for (auto c:word)
    {
        if (curr->children[c-'a']==NULL)
            return rez;
        rez++;
        curr=curr->children[c-'a'];
    }
    return rez;
}

int main()
{
    root=new trienode();
    int cer;
    while (fin >> cer)
    {
        string s;
        fin >> s;
        if (cer==0)
            insertword(root,s);
        if (cer==1)
            trienode *node=deleteword(root,s);
        if (cer==2)
            cout << frecv(root,s) << '\n';
        if (cer==3)
            cout << pref(root,s) << '\n';
    }
    return 0;
}