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:
@@ -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)
|
||||
|
||||
11
09_dynamic_programming/python/02_longest_common_substring.py
Normal file
11
09_dynamic_programming/python/02_longest_common_substring.py
Normal 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)
|
||||
Reference in New Issue
Block a user