Cod sursa(job #2849270)

Utilizator DMR6476Erdic Dragos DMR6476 Data 14 februarie 2022 19:38:08
Problema Trie Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3 kb
#include <iostream>
#include<bits/stdc++.h>

using namespace std;
ifstream fin("trie.in");
ofstream fout("trie.out");
struct nod
{
    int sons = 0;
    nod* neighbours[30];
    int enderOfHowManyWords = 0;
    nod()
    {
        memset(neighbours,0,sizeof(neighbours));
        enderOfHowManyWords = 0;
        sons = 0;
    }
};
void addWord(nod* currNode,char word[])
{
    int sizeOfWord = strlen(word);
    for(int i = 0 ; i < sizeOfWord; i++)
    {
        int indexOfLetter = word[i] - 'a';
        if(currNode ->neighbours[indexOfLetter] == NULL)
        {
            nod* newNode = new nod();

            currNode ->neighbours[indexOfLetter] = newNode;
            currNode ->sons += 1;
            currNode  = currNode ->neighbours[indexOfLetter];
        }
        else
        {
            currNode = currNode ->neighbours[indexOfLetter];
        }
    }
    currNode ->enderOfHowManyWords += 1;

}
bool deleteWord(nod* &currNod, char word[],int index,nod* root)
{

    if(strlen(word) == index)
    {

        currNod ->enderOfHowManyWords -= 1;
        if(currNod ->enderOfHowManyWords == 0 && currNod ->sons == 0)
        {
            delete currNod;
            return true;
        }
        return false;
    }
    bool wasDeleted = deleteWord(currNod ->neighbours[word[index] - 'a'],word,index + 1,root);

    if(wasDeleted == true)
    {
        currNod ->neighbours[word[index] - 'a'] = NULL;
        currNod ->sons -= 1;
    }

    if(currNod ->sons == 0 && currNod ->enderOfHowManyWords == 0 && currNod != root)
    {
        delete currNod;
        return true;
    }
    return false;

}
void typeApp(nod* currNode, char word[])
{

    int sizeOfWord = strlen(word);
    int app = 0;
    for(int i = 0 ; i < sizeOfWord; i++)
    {
        int indexOfLetter = word[i] - 'a';
        if(currNode ->neighbours[indexOfLetter] == NULL)
        {
            i = sizeOfWord;
        }
        else
        {
            currNode = currNode ->neighbours[indexOfLetter];
            if(i == sizeOfWord - 1)
                app = currNode ->enderOfHowManyWords;
        }
    }
    fout<<app<<'\n';
}
void typeLongestSubsequence(nod* currNode,char word[])
{
    int sizeOfWord = strlen(word);
    int subsSize = 0;
    for(int i = 0; i < sizeOfWord; i++)
    {
        int indexOfLetter = word[i] - 'a';
        if(currNode ->neighbours[indexOfLetter] == NULL)
            break;
        else
        {
            currNode = currNode ->neighbours[indexOfLetter];
            ++subsSize;
        }
    }
    fout<<subsSize<<'\n';
}
int main()
{
    int command;
    char word[25];
    nod* root = new nod();
    while(fin>>command>>word)
    {
        if(command == 0)
            addWord(root,word);
        if(command == 1)
            deleteWord(root,word,0,root);
        if(command == 2)
            typeApp(root,word);
        if(command == 3)
            typeLongestSubsequence(root,word);
    }

    return 0;
}