Mercurial > lbo > hg > juliaplay
changeset 23:41c00af82d66
DP: shortest common super sequence
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Thu, 02 Mar 2023 23:06:32 +0100 |
parents | cbb6e979bbd5 |
children | d660fc20d950 |
files | julia/dp.jl |
diffstat | 1 files changed, 23 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/julia/dp.jl Thu Mar 02 20:51:07 2023 +0100 +++ b/julia/dp.jl Thu Mar 02 23:06:32 2023 +0100 @@ -379,3 +379,26 @@ end end end + +function shortest_common_supersequence(x::AbstractString, y::AbstractString, i=1, j=1)::Int + if i > length(x) + return length(y)-j+1 + end + if j > length(y) + return length(x)-i+1 + end + + # Case 1: current first letters identical + if x[i] == y[j] + return 1 + shortest_common_supersequence(x, y, i+1, j+1) + end + + # Case 2: current first letters are not identical: choose shortest supersequence from here. + + # take from x + xfirst = 1 + shortest_common_supersequence(x, y, i+1, j) + # take from y + yfirst = 1 + shortest_common_supersequence(x, y, i, j+1) + + min(xfirst, yfirst) +end