We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
if (Waterbolt* w = dynamic_cast(spell)) {
w->revealWaterpower(); // If the spell is of type Waterbolt, reveal its water power.
} else if (Thunderstorm* t = dynamic_cast(spell)) {
t->revealThunderpower(); // If the spell is of type Thunderstorm, reveal its thunder power.
} else if (Fireball* fi = dynamic_cast(spell)) {
fi->revealFirepower(); // If the spell is of type Fireball, reveal its fire power.
} else if (Frostbite* fr = dynamic_cast(spell)) {
fr->revealFrostpower(); // If the spell is of type Frostbite, reveal its frost power.
} else {
string spell_name = spell->revealScrollName(); // Extract the name of the spell from the spell.
string scroll_name = SpellJournal::read(); // Read the spell journal to get its content.
int LCS_2D_arr[spell_name.length()+1][scroll_name.length()+1]; // Create a 2D array to hold LCS values for comparison.
// Iterate through each character in the spell name.
for (int i = 0; i <= spell_name.length(); i++) {
// For each character in the spell name, iterate through each character in the spell journal.
for (int j = 0; j <= scroll_name.length(); j++) {
// If it is the first character in either of the strings, insert 0 because it's the first character.
if (i == 0 || j == 0) {
LCS_2D_arr[i][j] = 0;
}
// If the characters at the current positions in the spell name and the spell journal are equal.
else if (spell_name[i-1] == scroll_name[j-1]) {
LCS_2D_arr[i][j] = LCS_2D_arr[i-1][j-1] + 1; // Increment LCS value by 1.
} else {
// Otherwise, take the maximum of LCS values obtained so far.
LCS_2D_arr[i][j] = max(LCS_2D_arr[i-1][j], LCS_2D_arr[i][j-1]);
}
}
}
printf("%d\n", LCS_2D_arr[spell_name.length()][scroll_name.length()]); // Print the LCS value after the LCS matrix has been fully computed.
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Magic Spells
You are viewing a single comment's thread. Return to all comments →
if (Waterbolt* w = dynamic_cast(spell)) { w->revealWaterpower(); // If the spell is of type Waterbolt, reveal its water power. } else if (Thunderstorm* t = dynamic_cast(spell)) { t->revealThunderpower(); // If the spell is of type Thunderstorm, reveal its thunder power. } else if (Fireball* fi = dynamic_cast(spell)) { fi->revealFirepower(); // If the spell is of type Fireball, reveal its fire power. } else if (Frostbite* fr = dynamic_cast(spell)) { fr->revealFrostpower(); // If the spell is of type Frostbite, reveal its frost power. } else { string spell_name = spell->revealScrollName(); // Extract the name of the spell from the spell. string scroll_name = SpellJournal::read(); // Read the spell journal to get its content. int LCS_2D_arr[spell_name.length()+1][scroll_name.length()+1]; // Create a 2D array to hold LCS values for comparison.