UNB/ CS/ David Bremner/ teaching/ cs3383/ lectures/ 31.0-demos/ lcs.py
#!/usr/bin/env python3
def lcs(c,x,y,i,j):
  if (i < 1) or (j<1):
    return 0
  if c[i][j] == None:
    if x[i-1] == y[j-1]:
      c[i][j]=lcs(c,x,y,i-1,j-1)+1
    else:
      c[i][j] = max(lcs(c,x,y,i-1,j),
                    lcs(c,x,y,i,j-1))
  return c[i][j]
if __name__ == "__main__":

  x = "TIMBERLAKE"
  y = "FRUITCAKE"

  n = len(x)
  m = len(y)

  c = [ [ None for j in range(m+1) ] for i in range(n+1) ]

  print(lcs(c,x,y,n,m))