Skip to content

Commit d889159

Browse files
OleksandrDanylchenkoegonSchiele
authored andcommitted
Added C++ Solution for 9th chapter (egonSchiele#146)
* Added C++ Solution for 9th chapter * Fixed extra space * Fixed another extra space * Output style correction
1 parent 009689b commit d889159

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
4+
int main() {
5+
std::string wordA = "Fish", wordB = "Fosh";
6+
const size_t& aSize = wordA.size(), &bSize = wordB.size();
7+
8+
size_t** table = new size_t* [aSize];
9+
for (size_t l = 0; l < aSize; ++l)
10+
table[l] = new size_t[bSize]{}; // all values in table will be initialized as zero
11+
12+
for (size_t i = 0; i < aSize; ++i)
13+
for (size_t j = 0; j < bSize; ++j)
14+
if (wordA[i] == wordB[j])
15+
if (i > 0 && j > 0)
16+
table[i][j] = table[i - 1][j - 1] + 1;
17+
else
18+
table[i][j] = 1;
19+
else
20+
if (i > 0 && j > 0)
21+
table[i][j] = std::max(table[i - 1][j], table[i][j - 1]);
22+
else if (i == 0 && j > 0)
23+
table[i][j] = table[i][j - 1];
24+
else if (i > 0 && j == 0)
25+
table[i][j] = table[i - 1][j];
26+
else
27+
table[i][j] = 0;
28+
29+
for (size_t i = 0; i < aSize; ++i) {
30+
std::cout << "[ ";
31+
for (size_t j = 0; j < bSize; ++j)
32+
std::cout << table[i][j] << ' ';
33+
std::cout << ']' << std::endl;
34+
}
35+
36+
// [1 1 1 1]
37+
// [1 1 1 1]
38+
// [1 1 2 2]
39+
// [1 1 2 3]
40+
41+
system("pause");
42+
return 0;
43+
}

0 commit comments

Comments
 (0)