Cod sursa(job #3171763)

Utilizator GhiciCineRazvan Dumitriu GhiciCine Data 19 noiembrie 2023 15:49:38
Problema Trie Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.11 kb
#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include <sstream>

using namespace std;

class TrieNode {
public:
    unordered_map<char, TrieNode*> children;
    int count;

    TrieNode() {
        count = 0;
    }
};

class Trie {
private:
    TrieNode* root;

    bool removeHelper(TrieNode* node, string& word, int level) {
        if (node) {
            if (level == word.length()) {
                if (node->count > 0) {
                    node->count--;
                    return true;
                }
                return false;
            }

            char nextChar = word[level];
            if (node->children.find(nextChar) != node->children.end()) {
                bool shouldDeleteNode = removeHelper(node->children[nextChar], word, level + 1);

                if (shouldDeleteNode) {
                    if (node->children[nextChar]->count == 0) {
                        delete node->children[nextChar];
                        node->children.erase(nextChar);
                    }
                    return node->children.empty();
                }
            }
        }
        return false;
    }

public:
    Trie() {
        root = new TrieNode();
    }

    void insert(string word) {
        TrieNode* current = root;
        for (char c : word) {
            if (current->children.find(c) == current->children.end()) {
                current->children[c] = new TrieNode();
            }
            current = current->children[c];
        }
        current->count++;
    }

    void remove(string word) {
        if (word.empty())
            return;
        removeHelper(root, word, 0);
    }

    int search(string word) {
        TrieNode* current = root;
        for (char c : word) {
            if (current->children.find(c) == current->children.end()) {
                return 0;
            }
            current = current->children[c];
        }
        return current->count;
    }

    int longestPrefix(string word) {
        TrieNode* current = root;
        int length = 0;
        for (char c : word) {
            if (current->children.find(c) == current->children.end()) {
                break;
            }
            current = current->children[c];
            length++;
        }
        return length;
    }
};

int main() {
    Trie trie;

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

    string line;
    while(getline(fin, line)) {
        istringstream iss(line);
        int command;
        string word;
        iss >> command >> word;
        switch(command) {
            case 0:
                trie.insert(word);
                break;
            case 1:
                trie.remove(word);
                break;
            case 2:
                fout << trie.search(word) << endl;
                break;
            case 3:
                fout << trie.longestPrefix(word) << endl;
                break;
            default:
                break;
        }
    }

    // trie.insert("latitudine");
    // cout << trie.longestPrefix("lati") << endl;
    // trie.remove("latitudine");
    // cout << trie.longestPrefix("lati") << endl;
    return 0;
}