A pull request from cx (#164)

* The original code did not consider the exit condition when the stations cannot perfectly cover the states_needed.

* The code in 09_dynamic_programming/python/01_longest_common_subsequence.py is completed. And the 02_longest_common_substring.py is added.
This commit is contained in:
Pikachu
2022-11-19 04:49:50 +08:00
committed by GitHub
parent c029201ab1
commit 06a890c302
3 changed files with 43 additions and 18 deletions

View File

@@ -1,6 +1,13 @@
if word_a[i] == word_b[j]:
# The letters match.
cell[i][j] = cell[i-1][j-1] + 1
else:
# The letters don't match.
cell[i][j] = max(cell[i-1][j], cell[i][j-1])
dp_table_blue = ["b", "l", "u", "e"]
dp_table_clues = ["c", "l", "u", "e", "s"]
dp_table = [[0 for i in range(len(dp_table_blue))] for i in range(len(dp_table_clues))] # (5,4)
print(dp_table)
for i in range(0, len(dp_table_blue)):
for j in range(0, len(dp_table_clues)):
if dp_table_clues[j] == dp_table_blue[i]:
dp_table[j][i] = dp_table[j-1][i-1] + 1
else:
dp_table[j][i] = max(dp_table[j-1][i], dp_table[j][i-1])
print(dp_table)

View File

@@ -0,0 +1,11 @@
dp_table_blue = ["b", "l", "u", "e"]
dp_table_clues = ["c", "l", "u", "e", "s"]
dp_table = [[0 for i in range(len(dp_table_blue))] for i in range(len(dp_table_clues))] # (5,4)
print(dp_table)
for i in range(0, len(dp_table_blue)):
for j in range(0, len(dp_table_clues)):
if dp_table_clues[j] == dp_table_blue[i]:
dp_table[i][j] = dp_table[i-1][i-1] + 1
print(dp_table)