Magic Spells

Sort by

recency

|

114 Discussions

|

  • + 0 comments

    A sane way to describe the task:

    1. The only thing that matters is, that your code produces the correct output to std::out!
    2. for pointer passed to the counterspell() function which are derived, the matching method needs to be called (reveal*())
    3. for pointer passed to counterspell() function which are not derived, the number of common characters need to be printed (comparing the scroll name and the global journal string). This matches the number of lines the linux diff utility does not show (which are identical).
  • + 1 comment

    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.

  • + 1 comment
    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;
        }