Magic Spells in C++ - Hacker Rank Solution


Magic Spells in C++ - Hacker Rank Solution


Problem


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.



Input Format :

The wizard will read t scrolls, which are hidden from you.
Every time he casts a spell, it's passed as an argument to your counterspell function.

Constraints :

  • 1 <= t <= 100
  • 1 <= |s| <= 1000, where s is a scroll name.
  • Each scroll name s, consists of uppercase and lowercase.

Output Format :

After identifying the given spell, print its name and power.
If it is a generic spell, find a subsequence of letters that are contained in both the spell name and your spell journal. Among all such subsequences, find and print the length of the longest one on a new line.



Sample Input :

3
fire 5
AquaVitae 999 AruTaVae
frost 7

Sample Output :

Fireball: 5
6
Frostbite: 7

Explanation :

Fireball and Frostbite are common spell types.
AquaVitae is not, and when you compare it with AruTaVae in your spell journal, you get a sequence: AuaVae



Solution :


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Magic Spells in C++ - Hacker Rank 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) 
{
/* Magic Spells in C++ - Hacker Rank Solution START */
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
    if (Fireball *s = dynamic_cast<Fireball *>(spell)) 
    {
        s->revealFirepower();
    } 
    else if (Frostbite *s = dynamic_cast<Frostbite *>(spell)) 
    {
        s->revealFrostpower();
    } 
    else if (Thunderstorm *s = dynamic_cast<Thunderstorm *>(spell)) 
    {
        s->revealThunderpower();
    } 
    else if (Waterbolt *s = dynamic_cast<Waterbolt *>(spell)) 
    {
        s->revealWaterpower();
    } 
    else 
    {
        string scroll_name = spell->revealScrollName();
        string journal = SpellJournal::read();
        size_t s_size = scroll_name.size();
        size_t j_size = journal.size();

        if (s_size == 1 && j_size == 1 && scroll_name == journal) 
        {
            cout << 1 << endl;
        } 
        else 
        {
            vector<vector<size_t>> lcs_table(s_size + 1, vector<size_t>(j_size + 1));

            for (size_t i = 1; i <= s_size; ++i) 
            {
                for (size_t j = 1; j <= j_size; ++j) 
                {
                    if (scroll_name[i - 1] == journal[j - 1]) 
                    {
                        lcs_table[i][j] = lcs_table[i - 1][j - 1] + 1;
                    } 
                    else 
                    {
                        lcs_table[i][j] = max(lcs_table[i][j - 1], lcs_table[i - 1][j]);
                    } 
                }
            }
          cout << lcs_table[s_size][j_size] << endl;
        }
    }
/* Magic Spells in C++ - Hacker Rank Solution END */

}

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;
}





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.


Next Post Previous Post
1 Comments
  • Parth
    Parth Sunday, August 08, 2021

    I wish that somebody explains that else part where two string gives sub string, if any one pls explain it plssss

Add Comment
comment url