Problem p02745 (python)

Fixed submission:
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True  and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def cmp1(a,b):
    l = []
    for i in range(len(a)):
        ok = True
        for j in range(min(len(b),len(a)-i)):
            #ddprint("a {} b {} i {} j {}".format(a,b,i,j))
            #ddprint("aij {} bj {}".format(a[i+j],b[j]))
            if a[i+j]!='?' and b[j]!='?' and a[i+j]!=b[j]:
                ok = False
                break
        if ok:
            l.append(i)
    #ddprint("cmp1 a {} b {} l {}".format(a,b,l))
    return l

def chk3(mn,a,b,c,ab,ac,bc):
    for i in ab:
        v = max(len(a),len(b)+i)+len(c)
        mn = min(mn,v)
        for j in bc:
            if len(a)-i<=j or (j+i) in ac:
                v = max([len(a),len(b)+i,len(c)+i+j])
                mn = min(mn,v)
        for j in ac:
            if len(b)+i<=j:
                v = max(len(a),len(c)+j)
                mn = min(mn,v)

    for i in bc:
        v = max(len(b),len(c)+i)+len(a)
        mn = min(mn,v)
    return mn

def solve(a,b,c):
    ab = cmp1(a,b)
    ac = cmp1(a,c)
    bc = cmp1(b,c)
    ba = cmp1(b,a)
    ca = cmp1(c,a)
    cb = cmp1(c,b)
    mn = len(a)+len(b)+len(c)
    ddprint(mn)
    mn = chk3(mn,a,b,c,ab,ac,bc)
    ddprint(mn)
    mn = chk3(mn,a,c,b,ac,ab,cb)
    ddprint(mn)
    mn = chk3(mn,b,a,c,ba,bc,ac)
    ddprint(mn)
    mn = chk3(mn,b,c,a,bc,ba,ca)
    ddprint(mn)
    mn = chk3(mn,c,a,b,ca,cb,ab)
    ddprint(mn)
    mn = chk3(mn,c,b,a,cb,ca,ba)
    return mn

a = ins()
b = ins()
c = ins()
mn = solve(a,b,c)
print(mn)

Buggy submission (verdict: Runtime Error):
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True  and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def cmp1(a,b):
    l = []
    for i in range(len(a)):
        ok = True
        for j in range(len(a)-i):
            #ddprint("a {} b {} i {} j {}".format(a,b,i,j))
            #ddprint("aij {} bj {}".format(a[i+j],b[j]))
            if a[i+j]!='?' and b[j]!='?' and a[i+j]!=b[j]:
                ok = False
                break
        if ok:
            l.append(i)
    #ddprint("cmp1 a {} b {} l {}".format(a,b,l))
    return l

def cmp(a,b):
    l1 = cmp1(a,b)
    l2 = cmp1(b,a)
    l2m = [-x for x in l2 if x!=0]
    l1.extend(l2m)
    return l1

a = ins()
b = ins()
c = ins()
la = len(a)
lb = len(b)
lc = len(c)
ab = cmp(a,b)
ac = cmp(a,c)
bc = cmp(b,c)
ddprint(ab)
ddprint(ac)
ddprint(bc)
ba = [-x for x in ab]
ca = [-x for x in ac]
cb = [-x for x in bc]
mn = la+lb+lc
for i in ab:
    for j in ac:
        if lc+j<=i or lb+i<=j or (j-i) in bc:
            v = max([la,lb+i,lc+j]) + max(0,-i,-j)
            mn = min(mn,v)
ddprint(mn)
for i in ba:
    for j in bc:
        if lc+j<=i or la+i<=j or (j-i) in ac:
            v = max([la+i,lb,lc+j]) + max(0,-i,-j)
            mn = min(mn,v)
ddprint(mn)
for i in ca:
    for j in cb:
        if lb+j<=i or la+i<=j or (j-i) in ab:
            v = max([la+i,lb+j,lc]) + max(0,-i,-j)
            mn = min(mn,v)
ddprint(mn)
for i in ab:
    v = max(la,lb+i)+max(0,-i)+lc
    mn = min(mn,v)
ddprint(mn)
for i in ac:
    v = max(la,lc+i)+max(0,-i)+lb
    mn = min(mn,v)
ddprint(mn)
for i in bc:
    v = max(lb,lc+i)+max(0,-i)+la
    mn = min(mn,v)
print(mn)