Magic Spells

  • + 0 comments

    Although the memory's not deallocated in the main function, it's not a big issue in this test problem.

    void counterspell(Spell *spell) {    
        Fireball *foo1 = dynamic_cast<Fireball*>(spell);
        Frostbite *foo2 = dynamic_cast<Frostbite*>(spell);
        Waterbolt *foo3 = dynamic_cast<Waterbolt*>(spell);
        Thunderstorm *foo4 = dynamic_cast<Thunderstorm*>(spell);
        if(foo1 != NULL) {
            foo1->revealFirepower();
        }
        else if(foo2 != NULL) {
            foo2->revealFrostpower();
        }
        else if(foo3 != NULL) {
            foo3->revealWaterpower();
        }
        else if(foo4 != NULL) {
            foo4->revealThunderpower();
        }
        else {
            string X = spell->revealScrollName();
            string Y = SpellJournal::read();
            int m = X.size();
            int n = Y.size();
            int L[m+1][n+1];
            int i, j;
            for (i = 0; i <= m; i++) {
                for (j = 0; j <= n; j++) {
                    if (i == 0 || j == 0)
                        L[i][j] = 0; 
                    else if (X[i-1] == Y[j-1])
                        L[i][j] = L[i-1][j-1] + 1;
                    else
                        L[i][j] = max(L[i-1][j], L[i][j-1]);
                }
            }
            cout << L[m][n] << endl;            
        }
    }