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