How to resolve the algorithm Shortest common supersequence step by step in the Haskell programming language
How to resolve the algorithm Shortest common supersequence step by step in the Haskell programming language
Table of Contents
Problem Statement
The shortest common supersequence is a problem closely related to the longest common subsequence, which you can use as an external function for this task.
Given two strings
u
{\displaystyle u}
and
v
{\displaystyle v}
, find the shortest possible sequence
s
{\displaystyle s}
, which is the shortest common super-sequence of
u
{\displaystyle u}
and
v
{\displaystyle v}
where both
u
{\displaystyle u}
and
v
{\displaystyle v}
are a subsequence of
s
{\displaystyle s}
. Defined as such,
s
{\displaystyle s}
is not necessarily unique. Demonstrate this by printing
s
{\displaystyle s}
where
u
{\displaystyle u=}
“abcbdab” and
v
{\displaystyle v=}
“bdcaba”.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Shortest common supersequence step by step in the Haskell programming language
The provided Haskell code defines a function scs
that takes two lists of elements of type a
(which must be instances of the Eq
typeclass) as input and returns a new list that is the shortest common subsequence (SCS) of the two input lists.
Here's how the code works:
-
The
scs
function takes two lists,xss
andyss
, as input. -
It first checks if either list is empty. If
xss
is empty, it returnsyss
. Ifyss
is empty, it returnsxss
. -
If neither list is empty, it compares the first elements of the two lists (
x
andy
). -
If
x
andy
are equal, it means they are part of the SCS, so it prependsx
to the result of callingscs
recursively on the tails ofxss
andyss
(i.e.,xs
andys
). -
If
x
andy
are not equal, it means they are not part of the SCS. So, it calculates two possible SCSs:us
by callingscs
recursively onxs
andyss
, andvs
by callingscs
recursively onxss
andys
. -
It then chooses the shorter of
us
andvs
as the SCS and prepends the first element of the chosen list (x
ory
) to it. -
Finally, it returns the calculated SCS.
When you run the code with the input lists "abcbdab"
and "bdcaba"
, it calculates and prints the SCS, which is "abcd"
.
Source code in the haskell programming language
scs :: Eq a => [a] -> [a] -> [a]
scs [] ys = ys
scs xs [] = xs
scs xss@(x:xs) yss@(y:ys)
| x == y = x : scs xs ys
| otherwise = ws
where
us = scs xs yss
vs = scs xss ys
ws | length us < length vs = x : us
| otherwise = y : vs
main = putStrLn $ scs "abcbdab" "bdcaba"
You may also check:How to resolve the algorithm SEDOLs step by step in the F# programming language
You may also check:How to resolve the algorithm Bitwise operations step by step in the Tcl programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/PCG32 step by step in the 11l programming language
You may also check:How to resolve the algorithm AKS test for primes step by step in the Rust programming language
You may also check:How to resolve the algorithm Truth table step by step in the Ruby programming language