Shortest common supersequence: Difference between revisions

Added Ruby
(Added Ruby)
Line 166:
{{out}}
<pre>"abdcabdab"</pre>
 
=={{header|Ruby}}==
{{trans|Tcl}}
uses 'lcs' from [[Longest common subsequence#Ruby|here]]
<lang ruby>require 'lcs'
 
def scs(u, v)
lcs = lcs(u, v)
scs = ""
# Iterate over the characters until LCS processed
ui = vi = li = 0
while li < lcs.size
uc = u[ui]
vc = v[vi]
lc = lcs[li]
if uc==lc
if vc==lc
# Part of the LCS, so consume from all strings
scs << lc
ui += 1
li += 1
else
# char of u = char of LCS, but char of LCS v doesn't so consume just that
scs << vc
end
vi += 1
else
# char of u != char of LCS, so consume just that
scs << uc
ui += 1
end
end
# append remaining characters, which are not in common
scs << u[ui..-1] << v[vi..-1]
end
 
u = "abcbdab"
v = "bdcaba"
puts "SCS(#{u}, #{v}) = #{scs(u, v)}"</lang>
 
{{out}}
<pre>
SCS(abcbdab, bdcaba) = abcbdcaba
</pre>
 
=={{header|Tcl}}==
Anonymous user