HackerRank C++ Magic Spells




You are battling a powerful dark wizard. He casts his spells from a distance, giving you only a few seconds to react and conjure your counterspells. For a counterspell to be effective, you must first identify what kind of spell you are dealing with.

The wizard uses scrolls to conjure his spells, and sometimes he uses some of his generic spells that restore his stamina. In that case, you will be able to extract the name of the scroll from the spell. Then you need to find out how similar this new spell is to the spell formulas written in your spell journal.

Spend some time reviewing the locked code in your editor, and complete the body of the counterspell function.

Check Dynamic cast to get an idea of how to solve this challenge.



  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5. class Spell {
  6. private:
  7. string scrollName;
  8. public:
  9. Spell(): scrollName("") { }
  10. Spell(string name): scrollName(name) { }
  11. virtual ~Spell() { }
  12. string revealScrollName() {
  13. return scrollName;
  14. }
  15. };
  16. class Fireball : public Spell {
  17. private: int power;
  18. public:
  19. Fireball(int power): power(power) { }
  20. void revealFirepower(){
  21. cout << "Fireball: " << power << endl;
  22. }
  23. };
  24. class Frostbite : public Spell {
  25. private: int power;
  26. public:
  27. Frostbite(int power): power(power) { }
  28. void revealFrostpower(){
  29. cout << "Frostbite: " << power << endl;
  30. }
  31. };
  32. class Thunderstorm : public Spell {
  33. private: int power;
  34. public:
  35. Thunderstorm(int power): power(power) { }
  36. void revealThunderpower(){
  37. cout << "Thunderstorm: " << power << endl;
  38. }
  39. };
  40. class Waterbolt : public Spell {
  41. private: int power;
  42. public:
  43. Waterbolt(int power): power(power) { }
  44. void revealWaterpower(){
  45. cout << "Waterbolt: " << power << endl;
  46. }
  47. };
  48. class SpellJournal {
  49. public:
  50. static string journal;
  51. static string read() {
  52. return journal;
  53. }
  54. };
  55. string SpellJournal::journal = "";
  56. void counterspell(Spell *spell) {
  57. /* Enter your code here */
  58. if(typeid(*spell) == typeid(Fireball)) {
  59. ((Fireball*)spell)->revealFirepower();
  60. }
  61. else if(typeid(*spell) == typeid(Frostbite)) {
  62. ((Frostbite*)spell)->revealFrostpower();
  63. }
  64. else if(typeid(*spell) == typeid(Thunderstorm)) {
  65. ((Thunderstorm*)spell)->revealThunderpower();
  66. }
  67. else if(typeid(*spell) == typeid(Waterbolt)) {
  68. ((Waterbolt*)spell)->revealWaterpower();
  69. }
  70. else {
  71. string spellName = spell->revealScrollName();
  72. string spellJournal = SpellJournal::journal;
  73. // LCS Algorithm
  74. string &a = spellName, &b = spellJournal;
  75. vector<vector<int> > dp(a.size() + 1, vector<int>(b.size() + 1));
  76. for(int i = 1; i <= a.size(); i++) {
  77. for(int j = 1; j <= b.size(); j++) {
  78. if(a[i - 1] == b[j - 1])
  79. dp[i][j] = 1 + dp[i - 1][j - 1];
  80. else
  81. dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
  82. }
  83. }
  84. cout << dp[a.size()][b.size()] << endl;
  85. }
  86. }
  87. class Wizard {
  88. public:
  89. Spell *cast() {
  90. Spell *spell;
  91. string s; cin >> s;
  92. int power; cin >> power;
  93. if(s == "fire") {
  94. spell = new Fireball(power);
  95. }
  96. else if(s == "frost") {
  97. spell = new Frostbite(power);
  98. }
  99. else if(s == "water") {
  100. spell = new Waterbolt(power);
  101. }
  102. else if(s == "thunder") {
  103. spell = new Thunderstorm(power);
  104. }
  105. else {
  106. spell = new Spell(s);
  107. cin >> SpellJournal::journal;
  108. }
  109. return spell;
  110. }
  111. };
  112. int main() {
  113. int T;
  114. cin >> T;
  115. Wizard Arawn;
  116. while(T--) {
  117. Spell *spell = Arawn.cast();
  118. counterspell(spell);
  119. }
  120. return 0;
  121. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube