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.
- Prepare
- C++
- Inheritance
- Magic Spells
- Discussions
Magic Spells
Magic Spells
+ 0 comments You can manage your Android smartphone from a distance, making changes, installing or removing apps, and doing much more. The application’s most remarkable feature is its capacity to transfer data between your computer and phone, such as media files. Bear in mind that this process is quick and as painless as possible.
+ 0 comments Here is the optimized solution for c++
void counterspell(Spell *spell) { /* Enter your code here */ 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::journal; int m = scrollName.length(); int n = journal.length(); auto init_2D_array = [](int m, int n) { vector<vector<int>> arr(m + 1, vector<int>(n + 1, 0)); return arr; }; vector<vector<int>> dp = init_2D_array(m, n); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (scrollName[i - 1] == journal[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } cout << dp[m][n] << endl; } }
+ 0 comments Here are the solution of HackerRank Magic Spells in C++ Solution
Join Telegram Group for Updates Click Here
+ 0 comments Here's code passed magic spells
#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) { /* Enter your code here */ if (dynamic_cast<Fireball*>(spell)) { Fireball* fireball = dynamic_cast<Fireball*>(spell); fireball->revealFirepower(); } else if (dynamic_cast<Frostbite*>(spell)) { Frostbite* frostbite = dynamic_cast<Frostbite*>(spell); frostbite->revealFrostpower(); } else if (dynamic_cast<Thunderstorm*>(spell)) { Thunderstorm* thunderstorm = dynamic_cast<Thunderstorm*>(spell); thunderstorm->revealThunderpower(); } else if (dynamic_cast<Waterbolt*>(spell)) { Waterbolt* waterbolt = dynamic_cast<Waterbolt*>(spell); waterbolt->revealWaterpower(); } else { string scrollName = spell->revealScrollName(); string journal = SpellJournal::journal; int m = scrollName.length(); int n = journal.length(); int dp[m + 1][n + 1]; for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = 0; } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (scrollName[i - 1] == journal[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } cout << dp[m][n] << endl; } } 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; }
Load more conversations
Sort 94 Discussions, By:
Please Login in order to post a comment