Skip to content

Commit 48a013e

Browse files
authored
Merge pull request ashutosh97#56 from masakotoda/hacktoberfest
Magic Spell (Longest Common Subsequence)
2 parents 68ced2e + 2059e7a commit 48a013e

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

Magic-Spells-LCS/problem.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
This is from https://www.hackerrank.com/challenges/magic-spells/problem
2+
3+
You are battling a powerful dark wizard. He casts his spells from a distance, giving you only a few seconds to react and conjure
4+
your counterspells. For a counterspell to be effective, you must first identify what kind of spell you are dealing with.
5+
6+
The wizard uses scrolls to conjure his spells, and sometimes he uses some of his generic spells that restore his stamina. In that
7+
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
8+
to the spell formulas written in your spell journal.
9+
10+
11+
Input Format
12+
The wizard will read scrolls, which are hidden from you.
13+
Every time he casts a spell, it's passed as an argument to your counterspell function.
14+
15+
16+
Output Format
17+
After identifying the given spell, print its name and power.
18+
If it is a generic spell, find a subsequence of letters that are contained in both the spell name and your spell journal.
19+
Among all such subsequences, find and print the length of the longest one on a new line.

Magic-Spells-LCS/solution.cpp

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

0 commit comments

Comments
 (0)