Problem p03165 (python)

Fixed submission:
def main(): 
  s = input()
  t = input()

  LS = len(s)
  LT = len(t)
      
  DP = [[0 for i in range(LT + 1)] for j in range(LS + 1)]
  L = [[0 for i in range(LT)] for j in range(LS)]
  for i in range(LS):
    for j in range(LT):
      a = DP[i+1][j]
      b = DP[i][j+1]
      if max(a, b) == a:
        DP[i+1][j+1] = a
        L[i][j] = 1
      else:
        DP[i+1][j+1] = b
        L[i][j] = 2
        a = b
      if s[i] == t[j]:
        c = DP[i][j] + 1
        if max(a, c) == c:
          DP[i+1][j+1] = c
          L[i][j] = 3       
        
  Ans = ""
  j = LS - 1
  k = LT - 1
  x = DP[LS][LT]
  for i in range(x, 0, -1):
    while True:
      if L[j][k] == 3:
        Ans += t[k]
        j -= 1
        k -= 1
        break
      elif L[j][k] == 2:
        j -= 1
      else:
        k -= 1
  print(Ans[::-1])
main()

Buggy submission (verdict: Runtime Error):
def main(): 
  s = input()
  t = input()

  LS = len(s)
  LT = len(t)
      
  DP = [[0 for i in range(LT + 1)] for j in range(LS + 1)]
  L = [[0 for i in range(LT)] for j in range(LS)]
  for i in range(LS):
    for j in range(LT):
      a = DP[i+1][j]
      b = DP[i][j+1]
      c = DP[i][j] + 1
      if max(a, b) == a:
        DP[i+1][j+1] = a
        L[i][j] = 1
      else:
        DP[i+1][j+1] = b
        L[i][j] = 2
        a = b
      if s[i] == t[j]:
        if max(a, c) == c:
          DP[i+1][j+1] = c
          L[i][j] = 3       
        
  Ans = ""
  j = LS - 1
  k = LT - 1
  x = DP[j][k]
  for i in range(x, -1, -1):
    while True:
      if L[j][k] == 3:
        Ans += t[k]
        j -= 1
        k -= 1
        break
      elif L[j][k] == 2:
        j -= 1
      else:
        k -= 1
  print(Ans[::-1])
main()

Buggy submission (verdict: Runtime Error):
s = input()
t = input()

LS = len(s)
LT = len(t)

num = LT
for i in range(LT):
  if s[0] == t[i]:
    num = i
    break

if LS == 1:
  if num == LT:
    print("")
  else:
    print (s)
  exit()
    
DP = [[0 if i < num or j != 0 else 1 for i in range(LT)] for j in range(LS)]
T = [1 if i == num else 0 for i in range(LT)]
if num != LT:
  L = [s[0]]
else:
  L = [[""]]
for i in range(1, LS):
  ma = DP[i-1][0]
  flag = 0
  for j in range(LT):
    if flag == 1:
      T[j] = 0
    if s[i] == t[j] and T[j] == 0:
      if flag == 0:
        T[j] = 1
        flag = 1
      Lt = DP[i-1][j]
      if len(L[ma-1]) <= len(L[Lt-1]):
        L.append(L[Lt-1] + s[i])
        ma = len(L)
    elif ma < DP[i-1][j]:
      ma = DP[i-1][j]
    DP[i][j] = ma

print(L[ma-1])

Buggy submission (verdict: Runtime Error):
s = input()
t = input()

LS = len(s)
LT = len(t)

num = LT
for i in range(LT):
  if s[0] == t[i]:
    num = i
    break

if LS == 1:
  if num == LT:
    print("")
  else:
    print (s)
  exit()
    
DP = [0 if i < num else s[0] for i in range(LT)]
T = [1 if i == num else 0 for i in range(LT)]
for i in range(1, LS):
  ma = DP[0]
  flag = 0
  for j in range(LT):    
    if flag == 1:
      T[j] = 0
    if s[i] == t[j] and T[j] == 0:
      if flag == 0:
        T[j] = 1
        flag = 1
      ma = DP[j] + s[i]
    elif len(ma) < len(DP[j]):
      ma = DP[j]
    DP[j] = ma
print(DP[-1])

Buggy submission (verdict: Runtime Error):
s = input()
t = input()

LS = len(s)
LT = len(t)

num = LT
for i in range(LT):
  if s[0] == t[i]:
    num = i
    break

if LS == 1:
  if num == LT:
    print("")
  else:
    print (s)
  exit()
    
DP = [[0 if i < num or j != 0 else s[0] for i in range(LT)] for j in range(LS)]
T = [1 if i == num else 0 for i in range(LT)]
for i in range(1, LS):
  ma = DP[i-1][0]
  flag = 0
  for j in range(LT):
    if flag == 1:
      T[j] = 0
    if s[i] == t[j] and T[j] == 0:
      if flag == 0:
        T[j] = 1
        flag = 1   
      ma = DP[i-1][j] + s[i]
    elif len(ma) < len(DP[i-1][j]):
      ma = DP[i-1][j]
    DP[i][j] = ma
print(DP[-1][-1])