|
| 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 (Fireball *fb=dynamic_cast<Fireball*>(spell)) { |
| 67 | + fb->revealFirepower(); |
| 68 | + } |
| 69 | + else if (Frostbite *fz=dynamic_cast<Frostbite*>(spell)) { |
| 70 | + fz->revealFrostpower(); |
| 71 | + } |
| 72 | + else if (Thunderstorm *ts=dynamic_cast<Thunderstorm*>(spell)) { |
| 73 | + ts->revealThunderpower(); |
| 74 | + } |
| 75 | + else if (Waterbolt *wb=dynamic_cast<Waterbolt*>(spell)) { |
| 76 | + wb->revealWaterpower(); |
| 77 | + } |
| 78 | + else { // generic spell |
| 79 | + string spellN = spell->revealScrollName(); |
| 80 | + string spellJ = SpellJournal::read(); |
| 81 | + int m=spellN.length(); |
| 82 | + int n=spellJ.length(); |
| 83 | + int array[m+1][n+1]; |
| 84 | + |
| 85 | + // solve for LCS |
| 86 | + for (int i=0;i<=m;i++) array[i][0]=0; |
| 87 | + for (int j=0;j<=n;j++) array[0][j]=0; |
| 88 | + for (int i=1;i<=m;i++) { |
| 89 | + for (int j=1;j<=n;j++) { |
| 90 | + if (spellN[i-1] == spellJ[j-1]) |
| 91 | + array[i][j]=array[i-1][j-1]+1; |
| 92 | + else |
| 93 | + array[i][j]=max(array[i][j-1],array[i-1][j]); |
| 94 | + } |
| 95 | + } |
| 96 | + cout << array[m][n] << endl; |
| 97 | + } |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | +class Wizard { |
| 102 | +public: |
| 103 | + Spell *cast() { |
| 104 | + Spell *spell; |
| 105 | + string s; cin >> s; |
| 106 | + int power; cin >> power; |
| 107 | + if(s == "fire") { |
| 108 | + spell = new Fireball(power); |
| 109 | + } |
| 110 | + else if(s == "frost") { |
| 111 | + spell = new Frostbite(power); |
| 112 | + } |
| 113 | + else if(s == "water") { |
| 114 | + spell = new Waterbolt(power); |
| 115 | + } |
| 116 | + else if(s == "thunder") { |
| 117 | + spell = new Thunderstorm(power); |
| 118 | + } |
| 119 | + else { |
| 120 | + spell = new Spell(s); |
| 121 | + cin >> SpellJournal::journal; |
| 122 | + } |
| 123 | + return spell; |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +int main() { |
| 128 | + int T; |
| 129 | + cin >> T; |
| 130 | + Wizard Arawn; |
| 131 | + while(T--) { |
| 132 | + Spell *spell = Arawn.cast(); |
| 133 | + counterspell(spell); |
| 134 | + } |
| 135 | + return 0; |
| 136 | +} |
0 commit comments