mardi 12 mai 2020

Is there optimal way to find substring with reapiting pattern in a string in C++?

I am struggling with a problem that I need to solve for my online classes. Normally my teacher would gladly help but now there is no possibility to contact him. The problem is following:

If there are two identical substrings next to eachother we can compress that by writing it as (substring). So, we can write zz as (z) and zzzz as ((z)) or zzzzzzzzzzbb as (((z)))(z)(b). Given a compressed text check if compression was done correctly, if possible - decompress it and check if compression was done in an optimal way, possibly by optimal compressing it again.

I have already written a function to check whether compression was done correctly, decompressing function and optimal compressing but only for string with only one pattern (like gorgorgorgorgorgor or zzzzzz) and when the pattern is known.

std::string ifreapeats(std::string tes, std::string dotes){

int iloraz = dotes.length() / tes.length();
int pot2 = highestpow2(iloraz);
int roznica = iloraz - pot2;

std::string finale;

finale = "";

int logar = int(log(iloraz) / log(2));

    for(int i = 0; i < logar; ++i)
    {
        finale += '(';
    }

    finale += tes;

    for (int i = 0; i < logar; ++i)
    {
        finale += ')';
    }

    if (roznica == 1)
        return finale + tes;

    if (roznica == 0)
        return finale;

    std::string dotes2 = "";

    for (int i = 0; i < roznica; ++i)
    {
        dotes2 += tes;
    }

    finale += ifreapeats(tes, dotes2);

    return finale;}

Where highestpow2 returns higest power of 2 smaller or equal than given number. Now I don't know how to apply this to more complicated strings, where there is more than one pattern and there are sequences without a pattern. For example aabbbaabbbxyzyz. It should return back ((a)(b)b)x(yz). I guess finding longest patterned substring might work but I have no idea how to apply this. Also this is problem way harder than what we have done earlier on our IT class. If it is not problem I would like some explanation so I can help other students in my class (everyone is hoping I will manage to finish the problem and give them a hand too).

Sorry for some grammatical mistakes I possibly made. I also might have left some variables names in polish, for what I apologize too.

Aucun commentaire:

Enregistrer un commentaire