Magic Spells

Sort by

recency

|

113 Discussions

|

  • + 0 comments

    If anyone is interested in a recursive solution to the second part :) :

        //Get spell and journal name
        string spellName = spell->revealScrollName();
        string journal = SpellJournal::journal;  
    
        // Initialize the memoization table with -1 (indicating uncomputed)
        int n = spellName.size();
        int m = journal.size();
        //memo is used to keep track of the computation, avoiding usless computation
        vector<vector<int>> memo(n + 1, vector<int>(m + 1, -1)); 
    
        auto f = [&spellName, &journal, &memo](int n, int m, auto& f) -> int {
            if (n == 0 || m == 0) return 0; //Reached the end of one string
            if (memo[n][m] != -1) return memo[n][m]; //If already computed
            
            if (spellName[n - 1] == journal[m - 1]) { //Update memo adding the new computation
                memo[n][m] = 1 + f(n - 1, m - 1, f);
            } else {
                memo[n][m] = std::max(f(n - 1, m, f), f(n, m - 1, f)); //Recall f over n-1 and m-1 to gety the substring which return the  highest value
            }
            return memo[n][m]; // Return the computed result
        };
    
  • + 0 comments

    how did you all find out all the spell like thunderstorm and waterbolt where is it given

  • + 0 comments

    In the subsequence portion of the problem the test uses the spell name of "AquaVitae 999 AruTaVae" and a journel of "AruTaVae"

    I dont understand on how this is't 8.

  • + 0 comments
    if (Fireball* fireball = dynamic_cast<Fireball*>(spell)) {
            fireball->revealFirepower();
        }
        else if (Frostbite* frostbite = dynamic_cast<Frostbite*>(spell)) {
            frostbite->revealFrostpower();
        }
        else if (Thunderstorm* thunderstorm = dynamic_cast<Thunderstorm*>(spell)) {
            thunderstorm->revealThunderpower();
        }
        else if (Waterbolt* waterbolt = dynamic_cast<Waterbolt*>(spell)) {
            waterbolt->revealWaterpower();
        }
        else {
            string scrollName = spell->revealScrollName();
            string journal = SpellJournal::read();
            int n = scrollName.length();
            int m = journal.length();
    
            vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
    
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    if (scrollName[i - 1] == journal[j - 1]) {
                        dp[i][j] = dp[i - 1][j - 1] + 1;
                    }
                    else {
                        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
                    }
                }
            }
    
            cout << dp[n][m] << endl;
        }
    
  • + 0 comments

    Take my whiskey neat Solution

    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
    
    class Spell { 
        private:
            string scrollName;
        public:
            Spell(): scrollName("") { }
            Spell(string name): scrollName(name) { }
            virtual ~Spell() { }
            string revealScrollName() {
                return scrollName;
            }
    };
    
    class Fireball : public Spell { 
        private: int power;
        public:
            Fireball(int power): power(power) { }
            void revealFirepower(){
                cout << "Fireball: " << power << endl;
            }
    };
    
    class Frostbite : public Spell {
        private: int power;
        public:
            Frostbite(int power): power(power) { }
            void revealFrostpower(){
                cout << "Frostbite: " << power << endl;
            }
    };
    
    class Thunderstorm : public Spell { 
        private: int power;
        public:
            Thunderstorm(int power): power(power) { }
            void revealThunderpower(){
                cout << "Thunderstorm: " << power << endl;
            }
    };
    
    class Waterbolt : public Spell { 
        private: int power;
        public:
            Waterbolt(int power): power(power) { }
            void revealWaterpower(){
                cout << "Waterbolt: " << power << endl;
            }
    };
    
    class SpellJournal {
        public:
            static string journal;
            static string read() {
                return journal;
            }
    }; 
    string SpellJournal::journal = "";
    
    void counterspell(Spell *spell) {
        if(dynamic_cast<Fireball*>(spell)){
            Fireball* f=(Fireball*)spell;
            f->revealFirepower();
        }else if(dynamic_cast<Frostbite*>(spell)){
            Frostbite* f=(Frostbite*)spell;
            f->revealFrostpower();
        }else if(dynamic_cast<Thunderstorm*>(spell)){
            Thunderstorm* f=(Thunderstorm*)spell;
            f->revealThunderpower();
        }else if(dynamic_cast<Waterbolt*>(spell)){
            Waterbolt* f=(Waterbolt*)spell;
            f->revealWaterpower();
        }else{
            //cout<<"here"<<endl;
        string s2=spell->revealScrollName();
        string s1=SpellJournal::journal;
        vector<vector<int> > dp(s1.size()+1,vector<int>(s2.size()+1,0));
        for(int i=1;i<=s1.size();i++){
            for(int j=1;j<=s2.size();j++){
                if(s1[i-1]==s2[j-1]){
                    dp[i][j]=dp[i-1][j-1] +1;
                }else{
                    dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
                }
            }
        }
        cout<<dp[s1.size()][s2.size()]<<endl; 
        }
        
      /* Enter your code here */
    
    }
    
    class Wizard {
        public:
            Spell *cast() {
                Spell *spell;
                string s; cin >> s;
                int power; cin >> power;
                if(s == "fire") {
                    spell = new Fireball(power);
                }
                else if(s == "frost") {
                    spell = new Frostbite(power);
                }
                else if(s == "water") {
                    spell = new Waterbolt(power);
                }
                else if(s == "thunder") {
                    spell = new Thunderstorm(power);
                } 
                else {
                    spell = new Spell(s);
                    cin >> SpellJournal::journal;
                }
                return spell;
            }
    };
    
    int main() {
        int T;
        cin >> T;
        Wizard Arawn;
        while(T--) {
            Spell *spell = Arawn.cast();
            counterspell(spell);
        }
        return 0;
    }