File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
09_dynamic_programming/с++ Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments