Cod sursa(job #3251554)

Utilizator redstonegamer22Andrei Ion redstonegamer22 Data 26 octombrie 2024 11:02:44
Problema Trie Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.86 kb
#include <bits/stdc++.h>

using namespace std;

struct TrieNode {
    int frecventa;
    map<char, TrieNode*> copii;

    TrieNode() {
        frecventa = 0;
    }
};

struct Trie {
    TrieNode* root;

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

    TrieNode* insert(TrieNode *r, string& s, int ind) {
        if(r == nullptr) {
            r = new TrieNode();
        }

        if(s.size() == ind) {
            r->frecventa += 1;
            return r;
        }

        char current_char = s[ind];

        r->copii[current_char] = insert(r->copii[current_char], s, ind + 1);

        return r;
    }

    void insert(string s) {
        root = insert(root, s, 0);
    }

    int _search(TrieNode* nod, string& s, int ind) {
        if(nod == nullptr) return 0;

        if(s.size() == ind) return nod->frecventa;

        char current_char = s[ind];

        if(nod->copii.count(current_char) == 0) {
            return 0;
        }
        return _search(nod->copii[current_char], s, ind + 1);
    }

    int search(string s) {
        return _search(root, s, 0);
    }

    TrieNode* erase(TrieNode* nod, string& s, int ind) {
        if(nod == nullptr) return nullptr;

        if(s.size() == ind) {
            nod->frecventa -= 1;
            nod->frecventa = max(nod->frecventa, 0);

            if(nod->copii.size() == 0 && nod->frecventa == 0) {
                delete nod;
                return nullptr;
            }

            return nod;
        }

        char current_char = s[ind];

        TrieNode* copil = erase(nod->copii[current_char], s, ind + 1);

        if(copil != nullptr) {
            nod->copii[current_char] = copil;
        } else {
            nod->copii.erase(current_char);
        }

        if(nod->copii.size() == 0 && nod->frecventa == 0) {
            delete nod;
            return nullptr;
        }

        return nod;
    }

    void erase(string s) {
        root = erase(root, s, 0);
    }

    string find_lcp(string s) {
        string prefix = "";
        TrieNode* current_node = root;

        for(int i = 0; i < s.size() && current_node != nullptr; i++) {
            char current_char = s[i];

            if(current_node->copii.count(current_char) == 0) {
                break;
            }

            prefix += current_char;
            current_node = current_node->copii[current_char];
        }

        return prefix;
    }
};

#ifndef LOCAL
ifstream in("trie.in");
ofstream out("trie.out");

#define cin in
#define cout out
#endif // LOCAL

int main() {
    int t; string s;

    Trie trie;

    while(cin >> t >> s) {
        // citim pana la finalul fisierului

        if(t == 0) {
            trie.insert(s);
        }
        if(t == 1) {
            trie.erase(s);
        }
        if(t == 2) {
            cout << trie.search(s) << '\n';
        }

        if(t == 3) {
            cout << trie.find_lcp(s).size() << '\n';
        }
    }
}