Cod sursa(job #3173552)

Utilizator allee69Aldea Alexia allee69 Data 23 noiembrie 2023 09:09:39
Problema Trie Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.79 kb
#include <unordered_map>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

struct TrieNode {
    unordered_map<char, TrieNode*> children;
    int count;
};

TrieNode* createNode() {
    TrieNode* node = new TrieNode();
    node->count = 0;
    return node;
}

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 && node->children[nextChar]->children.empty()) {
                    delete node->children[nextChar];
                    node->children.erase(nextChar);
                }
                return node->children.empty();
            }
        }
    }
    return false;
}

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

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

int search(TrieNode* root, 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(TrieNode* root, 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() {
    TrieNode* root = createNode();

    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:
                insert(root, word);
                break;
            case 1:
                remove(root, word);
                break;
            case 2:
                fout << search(root, word) << endl;
                break;
            case 3:
                fout << longestPrefix(root, word) << endl;
                break;
            default:
                break;
        }
    }

    return 0;
}