Problem p03165 (python)

Fixed submission:
s=[ord(ch)-97 for ch in input()]
t=[ord(ch)-97 for ch in input()]

# minimize
mnmz={ch for ch in s} & {ch for ch in t}
if len(mnmz)==0:
    print("")
    exit()
s=[ z for z in s if z in mnmz ]
t=[ z for z in t if z in mnmz ]
if len(s)>len(t): s, t = t, s

# init
dp=[[0]*len(s) for _ in range(len(t))]

#dp frame init
for i in range(len(s)): 
    if s[i]==t[0]:
        for j in range(i, len(s)):
            dp[0][j]=1
        break
for i in range(len(t)): 
    if s[0]==t[i]:
        for j in range(i, len(t)):
            dp[j][0]=1
        break
#dp
for i in range(1,len(t)):
    for j in range(1, len(s)):
        if s[j]==t[i]:
            dp[i][j]=dp[i-1][j-1]+1
        else: dp[i][j]=(dp[i][j-1] if dp[i][j-1]>dp[i-1][j] else dp[i-1][j])

#recover string
res=[]
j=len(s)-1
i=len(t)-1
while i>=0 and j>=0:
    if s[j]==t[i]: #and dp[i][j]==dp[i-1][j-1]+1:
        res.append(s[j])
        i=i-1
        j=j-1
        continue
    if i==0 and j==0: break
    if j>0 and dp[i][j]==dp[i][j-1]:
        j=j-1
    else: i=i-1
# last letter (first acually)


print("".join(chr(i+97) for i in reversed(res)))

Buggy submission (verdict: Runtime Error):
s=[ord(ch) for ch in input()]
t=[ord(ch) for ch in input()]
# 
#print("#")
 
# minimize
mnmz={i for i in s} & {j for j in t}
if len(mnmz)==0:
    print("")
    exit()
s=[ z for z in s if z in mnmz]
t=[ z for z in t if z in mnmz]
if len(s)>len(t): s, t = t, s
#print(s)
#print(t)
# init
dp=[0]*len(s)
res={}
#dp
for i in range(len(t)):
    #print(dp)
    prev=dp[0]
    if dp[0]==0 and t[i]==s[0]:
        dp[0]=1
        res[1]=[s[0]]
    acc={}
    for j in range(1, len(s)):
        temp = max(dp[j-1], dp[j])
        if s[j]==t[i]:
            if temp<prev+1:
                temp=prev+1
                acc[temp]=res[prev].copy()+[s[j]]
        prev, dp[j] = dp[j], temp
    res.update(acc)
    if dp[-1]==len(s): break
 
#print(dp)
 
print("".join([ chr(i) for i in res[dp[-1]]]))

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

# minimize
mnmz={ch for ch in s} & {ch for ch in t}
if len(mnmz)==0:
    print("")
    exit()
s="".join([ z for z in s if z in mnmz])
t="".join([ z for z in t if z in mnmz])
if len(s)>len(t): s, t = t, s

# init
dp=[0]*len(s)
res={}
#dp
for i in range(len(t)):
    #print(dp)
    prev=dp[0]
    if dp[0]==0 and t[i]==s[0]:
        dp[0]=1
        res[1]=[s[0]]
        collector={}
    for j in range(1, len(s)):
        temp = max(dp[j-1], dp[j])
        if s[j]==t[i]:
            if temp<prev+1:
                temp=prev+1
                if temp not in res: res[temp]=[]
                collector[temp]=res[prev]+[s[j]]
        prev, dp[j] = dp[j], temp
    res.update(collector)
    if dp[-1]==len(s): break

#print(dp)

print("".join(res[dp[-1]]))

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

# minimize
mnmz={ch for ch in s} & {ch for ch in t}
if len(mnmz)==0:
    print("")
    exit()
s="".join([ z for z in s if z in mnmz])
t="".join([ z for z in t if z in mnmz])
if len(s)>len(t): s, t = t, s

# init
dp=[0]*len(s)
res={}
#dp
for i in range(len(t)):
    #print(dp)
    prev=dp[0]
    if dp[0]==0 and t[i]==s[0]:
        dp[0]=1
        res[1]=[s[0]]
        collector={}
    for j in range(1, len(s)):
        temp = max(dp[j-1], dp[j])
        if s[j]==t[i]:
            if temp<prev+1:
                temp=prev+1
                if temp not in res: res[temp]=[]
                collector[temp]=res[prev]+[s[j]]
        prev, dp[j] = dp[j], temp
    res.update(collector)
    if dp[-1]==len(s): break

#print(dp)

print("".join(res[dp[-1]]))

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

# minimize
mnmz={ch for ch in s} & {ch for ch in t}
if len(mnmz)==0:
    print("")
    exit()
s="".join([ z for z in s if z in mnmz])
t="".join([ z for z in t if z in mnmz])
if len(s)>len(t): s, t = t, s
#print(s)
#print(t)
# init
dp=[0]*len(s)
res={}
#dp
for i in range(len(t)):
    #print(dp)
    prev=dp[0]
    if dp[0]==0 and t[i]==s[0]:
        dp[0]=1
        res[1]=[s[0]]
    for j in range(1, len(s)):
        temp = max(dp[j-1], dp[j])
        if s[j]==t[i]:
            if temp<prev+1:
                temp=prev+1
                res[temp]=res[prev]+[s[j]]
        prev, dp[j] = dp[j], temp
    if dp[len(s)-1]==len(s): break

#print(dp)

print("".join(res[dp[len(s)-1]]))

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

# minimize
mnmz={ch for ch in s} & {ch for ch in t}
if len(mnmz)==0:
    print("")
    exit()
s="".join([ z for z in s if z in mnmz])
t="".join([ z for z in t if z in mnmz])
if len(s)>len(t): s, t = t, s
#print(s)
#print(t)
# init
dp=[0]*len(s)
res={}
#dp
for i in range(len(t)):
    #print(dp)
    prev=dp[0]
    if dp[0]==0 and t[i]==s[0]:
        dp[0]=1
        res[1]=[s[0]]
    for j in range(1, len(s)):
        temp = max(dp[j-1], dp[j])
        if s[j]==t[i]:
            if temp<prev+1:
                temp=prev+1
                res[temp]=res[prev]+[s[j]]
        prev, dp[j] = dp[j], temp
    if dp[len(s)]==len(s): break

#print(dp)

print("".join(res[dp[len(s)-1]]))

Buggy submission (verdict: Runtime Error):
s=input()
t=input()
# minimize
mnmz={ch for ch in s} & {ch for ch in t}
if len(mnmz)==0:
    print("")
    exit()
s="".join([ z for z in s if z in mnmz])
t="".join([ z for z in t if z in mnmz])
# init
dp=[0]*len(s)
res={}
#dp
for i in range(len(t)):
    prev=dp[0]
    if dp[0]==0 and t[i]==s[0]:
        dp[0]=1
        res[1]=[s[0]]
    for j in range(1, len(s)):
        temp = max(dp[j-1], dp[j])
        if s[j]==t[i]:
            if temp<prev+1:
                temp=prev+1
                res[temp]=res[prev]+[s[j]]
        prev, dp[j] = dp[j], temp

print("".join(res[dp[len(s)-1]]))