In this HackerRank Magic Spells problem in c++, You need to complete the body of the counterspell function.

HackerRank Magic Spells in C++ problem solution

 HackerRank Magic Spells in C++ problem solution

    string oldJournal=SpellJournal::read();
    string spellname=spell->revealScrollName();

    static int LCS[1001][1001];

    if (spellname.length())//find longest cmmon subsequence print, return
    {
        for (int i=0;i<1000;i++)
        {
            LCS[0][i]=LCS[i][0]=0;
        }
        for (int i=0;i<spellname.length();i++)
        {
            for (int j=0;j<oldJournal.length();j++)
            {
                if (spellname[i]==oldJournal[j])
                    LCS[i+1][j+1] = LCS[i][j] + 1;
                else
                    LCS[i+1][j+1] = max(LCS[i+1][j], LCS[i][j+1]);
            }
        }
        cout << LCS[spellname.length()][oldJournal.length()]<<endl;

        return;
    }

    try
    {
        Fireball* s = dynamic_cast<Fireball*>(spell);
        if (s!=NULL)
        {
            s->revealFirepower();
            return;
        }
    }
    catch( ... )
    {}

    try
    {
        Waterbolt* s = dynamic_cast<Waterbolt*>(spell);
        if (s!=NULL)
        {
            s->revealWaterpower();
            return;
        }
    }
    catch( ... )
    {}

    try
    {
        Thunderstorm* s = dynamic_cast<Thunderstorm*>(spell);
        if (s!=NULL)
        {
            s->revealThunderpower();
            return;
        }
    }
    catch( ... )
    {}

    try
    {
        Frostbite* s = dynamic_cast<Frostbite*>(spell);
        if (s!=NULL)
        {
            s->revealFrostpower();
            return;
        }
    }
    catch( ... )
    {}