Cod sursa(job #3322002)

Utilizator savudariusSavu Darius savudarius Data 12 noiembrie 2025 09:43:34
Problema Trie Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.3 kb
#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <fstream>
using namespace std;
ifstream fin("trie.in");
ofstream fout("trie.out");

class TrieNode
{
public:
    TrieNode* children[26];

    bool isLeaf;
    int valid;
    int appearance;
    TrieNode()
    {
        isLeaf = false;
        appearance = 0;
        valid = 0;
        for (int i = 0; i < 26; i++)
        {
            children[i] = nullptr;
        }
    }

};
// Method to insert a key into the Trie
void insert(TrieNode* root, const string& key)
{

    // Initialize the curr pointer with the root node
    TrieNode* curr = root;

    // Iterate across the length of the string
    for (char c : key)
    {

        // Check if the node exists for the
        // current character in the Trie
        if (curr->children[c - 'a'] == nullptr)
        {

            // If node for current character does
            // not exist then make a new node
            TrieNode* newNode = new TrieNode();

            // Keep the reference for the newly
            // created node
            curr->children[c - 'a'] = newNode;
        }
        // Move the curr pointer to the
        // newly created node
        curr = curr->children[c - 'a'];
        curr->valid++;
        //cout<<curr->valid<<" ";
    }

    // Mark the end of the word
    curr->isLeaf = true;
    curr->appearance++;
}

// Method to search a key in the Trie
int search(TrieNode* root, const string& key)
{

    // Initialize the curr pointer with the root node
    TrieNode* curr = root;

    // Iterate across the length of the string
    for (char c : key)
    {

        // Check if the node exists for the
        // current character in the Trie
        if (curr->children[c - 'a'] == nullptr)
            break;

        // Move the curr pointer to the
        // already existing node for the
        // current character
        curr = curr->children[c - 'a'];
    }

    // Return true if the word exists
    // and is marked as ending
    return curr->appearance;
}

// Method to check if a prefix exists in the Trie
int Prefix(TrieNode* root, const string& prefix)
{ 
    // root -> l 1 -> a 2 -> t 3
    TrieNode* curr = root;
    int count=0;
    for (char c : prefix)
    {
        if (curr->children[c - 'a'] == nullptr || curr->children[c-'a']->valid==0)
            break;

        curr = curr->children[c - 'a'];
        count++;
    }

    // If we reach here, the prefix exists in the Trie
    return count;
}
void RemoveAppearance(TrieNode* root, const string& key)
{
    TrieNode* curr = root;

    for (char c : key)
    {
        curr = curr->children[c - 'a'];
        curr->valid--;
        //cout << curr->valid << " ";
    }
    curr->appearance--;
}

int main()
{
    TrieNode* root = new TrieNode();
    pair <int, string> p;
    while (fin >>p.first >> p.second) 
    {
        if (p.first == 0)
            insert(root, p.second);
        else if (p.first == 1)
            RemoveAppearance(root, p.second);
        else if (p.first == 2)
            fout << search(root, p.second) << endl;
        else if (p.first == 3)
            fout << Prefix(root, p.second) << endl;
    }
    fin.close();
    fout.close();
    return 0;
}