Cod sursa(job #3320767)

Utilizator n0bmasterMihut Matei n0bmaster Data 7 noiembrie 2025 11:29:23
Problema Trie Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.76 kb
#include <bits/stdc++.h>
using namespace std;

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

class Trie {
private:
    struct node {
        int passcount = 0;
        int endcount = 0;
        array<node*, 26> children = {};
    };
    static int get_id(char c) { return c - 'a'; }

    node* root = new node;

public:
    void insert(string_view s) {
        auto aux = root;
        aux->passcount++;
        for (auto ch : s) {
            int i = get_id(ch);
            if (!aux->children[i]) aux->children[i] = new node;
            aux = aux->children[i];
            aux->passcount++;
        }
        aux->endcount++;
    }

    int contains(string_view s) {
        auto aux = root;
        if (!aux) return false;
        for (auto ch : s) {
            int i = get_id(ch);
            if (!aux->children[i]) return false;
            aux = aux->children[i];
        }
        return aux->endcount;
    }

    string longest_prefix(string_view s) {
        auto aux = root;
        string ans;
        if (!aux) return ans;
        for (auto ch : s) {
            int i = get_id(ch);
            if (!aux->children[i]) break;
            aux = aux->children[i];     
            ans.push_back(ch);
        }
        return ans;
    }

private:
    void erase_rec(node*& cur, string_view s, int depth, bool is_root) {
        if (!cur) return; // safety
        if (depth == (int)s.size()) {
            cur->endcount--;
            cur->passcount--;
        } else {
            int i = get_id(s[depth]);
            erase_rec(cur->children[i], s, depth + 1, false);
            cur->passcount--;
        }

        if (cur->passcount > 0 || cur->endcount > 0) return;

        for (auto child : cur->children) {
            if (child) return;
        }

        if (!is_root) {
            delete cur;
            cur = nullptr;
        } 
    }

public:
    bool erase(string_view s) {
        if (!contains(s)) return false;
        erase_rec(root, s, 0, true);
        return true;
    }
};

int main() {
    ios::sync_with_stdio(false);
    // cin.tie(nullptr);

    Trie trie;
	// trie.insert("mare");
	// cout<<trie.contains("mare")<<endl;
	// trie.insert("mare");
	// cout<<trie.contains("mare")<<endl;
    int cmd;
    string word;
    while (fin >> cmd >> word) {
        switch (cmd) {
            case 0: { 
                trie.insert(word);
                break;
            }
            case 1: { 
                trie.erase(word);
                break;
            }
            case 2: { 
                fout << (trie.contains(word) ? 1 : 0) << '\n';
                break;
            }
            case 3: { 
                fout << trie.longest_prefix(word).size() << '\n';
                break;
            }
            default:
                break;
        }
    }
    return 0;
}