-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhr_magic_spells.cpp
More file actions
148 lines (139 loc) · 3.88 KB
/
Copy pathhr_magic_spells.cpp
File metadata and controls
148 lines (139 loc) · 3.88 KB
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
#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 */
Fireball *fir = dynamic_cast<Fireball *>(spell);
Frostbite *fro = dynamic_cast<Frostbite *>(spell);
Thunderstorm *thu = dynamic_cast<Thunderstorm *>(spell);
Waterbolt *wat = dynamic_cast<Waterbolt *>(spell);
if (fir != NULL) {
fir->revealFirepower();
} else if (fro != NULL) {
fro->revealFrostpower();
} else if (thu != NULL) {
thu->revealThunderpower();
} else if (wat != NULL) {
wat->revealWaterpower();
} else {
string roots[2] = {spell->revealScrollName(), SpellJournal::read()};
int maxh = min(roots[0].size(), roots[1].size());
vector<int> seqi, seqj;
for (int i = 0; i < roots[0].size(); ++i) {
for (int j = 0; j < roots[1].size(); ++j) {
if (roots[0][i] == roots[1][j]) {
seqi.push_back(i);
seqj.push_back(j);
}
}
}
vector<int> mati[maxh], matj[maxh];
maxh = -1;
for (int j = 0; j < seqj.size(); ++j) {
int curi = seqi[j], curj = seqj[j];
int bh = 0;
for (int h = maxh; h >= 0; --h) {
for (int k = 0; k < matj[h].size(); ++k) {
if (curj > matj[h][k] and curi > mati[h][k]) {
bh = h+1;
break;
}
}
if (bh > 0)
break;
}
mati[bh].push_back(curi);
matj[bh].push_back(curj);
if (bh > maxh)
maxh = bh;
}
cout << maxh+1 << '\n';
} // else spell generic
} // counterspell
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;
}