Question: How would you write a memoized, iterative version of the longest common subsequence algorithm? A memoized, recursive version looks as follows: def lcsLena (X,Y): gamma
How would you write a "memoized", iterative version of the longest common subsequence algorithm?
A memoized, recursive version looks as follows:

def lcsLena (X,Y): gamma make Table (len (X)+1 len (Y)+1) #cache for length values, with extra 0th row & column. return c(X,Y,len (x),len (Y) ,gamma) def c cache) if cache Ci] [j None if i 0 or j 0: cache [i] Cj] 0 elif xli-1] YDj-1]: We need to offset both i and j, because of the extra values cache [i] Cj] c (x,Y,i-1,j-1, cache 1 else a c cache) b c(x,Y,i, j 1, cache) if a b: cache [i] [1] a else cache [i] Cj] b return cache Llen(X)] Clen (Y)]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
