Cod sursa(job #3041394)

Utilizator annna7Pecheanu Anna annna7 Data 31 martie 2023 13:48:01
Problema Trie Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.88 kb
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

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

const int ALPHABET_LENGTH = 26;

inline int getFirstLetterIndex(char c) {
    return c - 'a';
}

class TrieNode {
public:
    TrieNode *children[ALPHABET_LENGTH];
    int totalSons;
    int wordAppearances;
    TrieNode() {
        for (auto & i : children) {
            i = nullptr;
        }
        totalSons = 0;
        wordAppearances = 0;
    }
};

/*
 * Daca nu exista un nod care sa contina cheia, se va crea un nod nou
 * Daca am terminat de procesat cuvantul, crestem numarul de aparitii al cuvantului respectiv
 * Daca nu am terminat de procesat cuvantul, apelam recursiv functia insert pe copilul corespunzator
 */
TrieNode* insert(TrieNode *root, string key) {
    if (root == nullptr) {
        root = new TrieNode();
    }
    root->totalSons++;
    if (key.length() == 0) {
        root->wordAppearances++;
    } else {
        int firstLetterIndex = getFirstLetterIndex(key[0]);
        root->children[firstLetterIndex] = insert(root->children[firstLetterIndex], key.substr(1));
    }
    return root;
}

TrieNode* remove(TrieNode *root, string key) {
    if (root == nullptr) {
        return nullptr;
    }
    if (key.length() == 0) {
        root->wordAppearances--;
    } else {
        int firstLetterIndex = getFirstLetterIndex(key[0]);
        root->children[firstLetterIndex] = remove(root->children[firstLetterIndex], key.substr(1));
    }
    root->totalSons--;
    if (root->totalSons == 0) {
        delete root;
        return nullptr;
    }
    return root;
}

int getWordAppearances(TrieNode *root, string key) {
    if (root == nullptr) {
        return 0;
    }
    if (key.length() == 0) {
        return root->wordAppearances;
    }
    int firstLetterIndex = getFirstLetterIndex(key[0]);
    return getWordAppearances(root->children[firstLetterIndex], key.substr(1));
}

int getLongestPrefixLength(TrieNode *root, string key) {
    if (root == nullptr || key.length() == 0) {
        return 0;
    }
    int firstLetterIndex = getFirstLetterIndex(key[0]);
    if (root->children[firstLetterIndex] == nullptr) {
        return 0;
    }
    return 1 + getLongestPrefixLength(root->children[firstLetterIndex], key.substr(1));
}

int main() {
    int queryType;
    string word;
    auto *root = new TrieNode();
    while (fin >> queryType >> word) {
        switch(queryType) {
            case 0: {
                root = insert(root, word);
                break;
            }
            case 1: {
                root = remove(root, word);
                break;
            }
            case 2: {
                fout << getWordAppearances(root, word) << '\n';
                break;
            }
            default: {
                fout << getLongestPrefixLength(root, word) << '\n';
            }
        }
    }
    fin.close();
    fout.close();
    return 0;
}