-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy path01_longest_common_subsequence.exs
51 lines (45 loc) · 1.21 KB
/
01_longest_common_subsequence.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
defmodule LCSubsequence do
def generate_table(word_a, word_b) do
word_a
|> Enum.with_index()
|> Enum.reduce(%{}, fn {_x, index}, acc ->
nested_map =
word_b
|> Enum.with_index()
|> Enum.reduce(%{}, fn {_x, index}, acc ->
Map.put(acc, index, 0)
end)
Map.put(acc, index, nested_map)
end)
end
def compute(table, word_a, word_b) do
for i <- 0..(length(word_a) - 1), reduce: table do
table ->
for j <- 0..(length(word_b) - 1), reduce: table do
table ->
if Enum.at(word_a, i) == Enum.at(word_b, j) do
value = (table[i - 1][j - 1] || 0) + 1
put_in(table[i][j], value)
else
first = table[i - 1][j] || 0
second = table[i][j - 1] || 0
put_in(table[i][j], max(first, second))
end
end
end
end
end
word_a = ["b", "l", "u", "e"]
word_b = ["c", "l", "u", "e", "s"]
result =
word_a
|> LCSubsequence.generate_table(word_b)
|> LCSubsequence.compute(word_a, word_b)
Enum.each(result, fn {_index, value} ->
IO.inspect(Map.values(value))
end)
# Output
# [0, 0, 0, 0, 0]
# [0, 1, 1, 1, 1]
# [0, 1, 2, 2, 2]
# [0, 1, 2, 3, 3]