개발 공부/알고리즘

HackerRank, c++, Magic Spells

그냥하는티스토리 2022. 11. 4. 10:49
728x90

AquaVitae is not, and when you compare it with AruTaVae in your spell journal, you get a sequence: AuaVae 

=> 이 라인때문에 당황함,,

결론은 dynamic_cast와 LCS(longgest common substring) 문제 였음;;

문제 이해 안되서 해답보고 이해한 문제;;

void counterspell(Spell *spell) {
    /* Enter your code here */
    Fireball* fireball;
    Frostbite* frostbite;
    Waterbolt* waterbolt;
    Thunderstorm* thunderstorm;
    Spell* spellPointer;
    
    if((fireball = dynamic_cast<Fireball*>(spell)))
    {
        fireball->revealFirepower();
    }
    else if((frostbite = dynamic_cast<Frostbite*>(spell)))
    {
        frostbite->revealFrostpower();
    }
    else if((waterbolt = dynamic_cast<Waterbolt*>(spell)))
    {
        waterbolt->revealWaterpower();
    }
    else if((thunderstorm = dynamic_cast<Thunderstorm*>(spell)))
    {
        thunderstorm->revealThunderpower();
    }
    else {
        string spellName = spell->revealScrollName();
        string journalName = SpellJournal::journal;
        int spellNameLen = spellName.length();
        int journalNameLen = journalName.length();
        
        vector< vector<int>> matrix(spellNameLen+1, vector<int>(journalNameLen+1));
        for(unsigned int i = 0; i<spellNameLen; i++)
        {
            for(unsigned int j = 0 ; j < journalNameLen; j++)
            {
                if(spellName[i] == journalName[j])
                {
                    matrix[i+1][j+1] = matrix[i][j] + 1;
                }
                else 
                {
                    matrix[i+1][j+1] = max(matrix[i][j+1], matrix[i+1][j]);
                }
            }
        }
        cout << matrix[spellNameLen][journalNameLen] << endl;
        
    }

}
728x90