Cod sursa(job #2615518)

Utilizator Constantin.Dragancea Constantin Constantin. Data 14 mai 2020 18:45:09
Problema Trie Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.13 kb
#include <bits/stdc++.h>
using namespace std;

class Trie {
private:    
    struct Node{
        char c;
        int count;
        vector <Node*> next;
        Node* father;
        
        Node(char c_ = '#', Node* father_ = nullptr): c(c_), count(0), next(vector <Node*>(26, nullptr)), father(father_) {}
        ~Node(){
            c = 0;
            count = 0;
            next.clear();
            father = nullptr;
        }
    } *root;
    
    bool is_deleteable(Node* node){
        bool flag = (node->count > 0);
        for (int i=0; i<26; i++){
            flag |= (node->next[i] != nullptr);
        }
            
        return !flag;
    }
    
    
    
public:

    void print_all(){
        queue < pair<Node*, string> > Q;
        Q.push({root, ""});
        while (!Q.empty()){
            Node* node = Q.front().first;
            string word = Q.front().second;
            Q.pop();
            //if (node->count > 0)
                cout << word << '\n';
            for (int i=0; i<26; i++){
                if (node->next[i]) Q.push({node->next[i], word + char(i + 'a')});
            }
        }
    }
    Trie(): root(new Node('#')) {}
    
    void insert(string word) {
        Node* current_node = root;
        unsigned i = 0;
        while (i < word.size()){
            if (current_node->next[word[i] - 'a'] == NULL){
                current_node->next[word[i] - 'a'] = new Node(word[i], current_node);
            }
            current_node = current_node->next[word[i] - 'a'];
            i++;
        }
        current_node->count++;
    }
    
    void erase(string word){
        Node* current_node = root;
        unsigned i = 0;
        while (i < word.size()){
            current_node = current_node->next[word[i] - 'a'];
            i++;
        }
        current_node->count--;
        while (is_deleteable(current_node)){
            Node* aux = current_node->father;
            for (i=0; i<26; i++){
                if (aux->next[i] == current_node)
                    aux->next[i] = nullptr;
            }
            delete current_node;
            current_node = aux;
        }
    }
    
    int count_appearences(string word) {
        Node * current_node = root;
        unsigned i = 0;
        while (i < word.size()){
            if (!current_node->next[word[i] - 'a'])
                return 0;
            current_node = current_node->next[word[i] - 'a'];
            i++;
        }
        return current_node->count;
    }
    
    int lcs(string word){
        Node * current_node = root;
        unsigned i = 0;
        while (i < word.size()){
            if (current_node->next[word[i] - 'a'] == nullptr)
                break;
            current_node = current_node->next[word[i] - 'a'];
            i++;
        }
        return i;
    }
};

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ifstream cin ("trie.in");
    ofstream cout ("trie.out");
    Trie t;
    int o; string s;
    while (cin >> o >> s){
        if (o == 0) t.insert(s);
        else if (o == 1) t.erase(s);
        else if (o == 2) cout << t.count_appearences(s) << '\n';
        else cout << t.lcs(s) << '\n';
    }
    return 0;
}