Problem p02745 (python)

Fixed submission:
# E - Three Substrings

a = list(input())
b = list(input())
c = list(input())

A, B, C = len(a), len(b), len(c)

M = max(A, B, C)
cons = 6*M
a_not_match_b = [False] * 2 * cons
b_not_match_c = [False] * 2 * cons
a_not_match_c = [False] * 2 * cons

def fill_match_list(d1, d2, l):
    for i in range(len(d1)):
        for j in range(len(d2)):
            if d1[i] != d2[j] and d1[i] != "?" and d2[j] != "?":
                l[i-j+cons] = True
    return l

a_not_match_b = fill_match_list(a, b, a_not_match_b)
b_not_match_c = fill_match_list(b, c, b_not_match_c)
a_not_match_c = fill_match_list(a, c, a_not_match_c)

ans = 3*M
for b_idx in range(-2*M, 2*M):
    for c_idx in range(-2*M, 2*M):
        if not a_not_match_b[b_idx + cons] and not a_not_match_c[c_idx + cons] and not b_not_match_c[c_idx - b_idx + cons]:
            ans = min(ans, max(A, B + b_idx, C + c_idx) - min(0, b_idx, c_idx))

print(ans)

Buggy submission (verdict: Runtime Error):
# E - Three Substrings

a = list(input())
b = list(input())
c = list(input())

A, B, C = len(a), len(b), len(c)

M = max(A, B, C)
cons = 3*M + 10
rel_match_b_against_a = [True] * 2 * cons
rel_match_b_against_c = [True] * 2 * cons
rel_match_c_against_a = [True] * 2 * cons

for rel_idx in range(-B+1, A):
    if rel_idx < 0:
        for i in range(min(A, rel_idx + B)):
            if a[i] != b[i + rel_idx] and a[i] != "?" and b[i + rel_idx] != "?":
                rel_match_b_against_a[rel_idx + cons] = False
                break
    else:
        for i in range(min(A - rel_idx, B)):
            if a[i + rel_idx] != b[i] and a[i + rel_idx] != "?" and b[i] != "?":
                rel_match_b_against_a[rel_idx + cons] = False
                break

for rel_idx in range(-B+1, C):
    if rel_idx < 0:
        for i in range(min(C, rel_idx + B)):
            if c[i] != b[i + rel_idx] and c[i] != "?" and b[i + rel_idx] != "?":
                rel_match_b_against_c[rel_idx + cons] = False
                break
    else:
        for i in range(min(C - rel_idx, B)):
            if c[i + rel_idx] != b[i] and c[i + rel_idx] != "?" and b[i] != "?":
                rel_match_b_against_c[rel_idx + cons] = False
                break

for rel_idx in range(-C+1, A):
    if rel_idx < 0:
        for i in range(min(A, rel_idx + C)):
            if a[i] != c[i + rel_idx] and a[i] != "?" and c[i + rel_idx] != "?":
                rel_match_c_against_a[rel_idx + cons] = False
                break
    else:
        for i in range(min(A - rel_idx, C)):
            if a[i + rel_idx] != c[i] and a[i + rel_idx] != "?" and c[i] != "?":
                rel_match_c_against_a[rel_idx + cons] = False
                break

ans = 3*M
for rel_idx_b_against_a in range(-2*M, 2*M):
    for rel_idx_c_against_a in range(-2*M, 2*M):
        if rel_match_b_against_a[rel_idx_b_against_a + cons] and rel_match_c_against_a[rel_idx_c_against_a + cons] and rel_match_b_against_c[rel_idx_b_against_a - rel_idx_c_against_a + cons]:
            ans = min(ans, max(A, B + rel_idx_b_against_a, C + rel_idx_c_against_a) - min(0, rel_idx_b_against_a, rel_idx_c_against_a))

print(ans)

Buggy submission (verdict: Runtime Error):
# E - Three Substrings

a = list(input())
b = list(input())
c = list(input())

A, B, C = len(a), len(b), len(c)

M = max(A, B, C)
cons = 3*M + 10
rel_match_b_against_a = [True] * 2 * cons
rel_match_b_against_c = [True] * 2 * cons
rel_match_c_against_a = [True] * 2 * cons

for rel_idx in range(-B+1, A):
    if rel_idx < 0:
        for i in range(min(A, rel_idx + B)):
            if a[i] != b[i + rel_idx] and a[i] != "?" and b[i + rel_idx] != "?":
                rel_match_b_against_a[rel_idx + cons] = False
                break
    else:
        for i in range(min(A - rel_idx, B)):
            if a[i + rel_idx] != b[i] and a[i + rel_idx] != "?" and b[i] != "?":
                rel_match_b_against_a[rel_idx + cons] = False
                break

for rel_idx in range(-B+1, C):
    if rel_idx < 0:
        for i in range(min(C, rel_idx + B)):
            if c[i] != b[i + rel_idx] and c[i] != "?" and b[i + rel_idx] != "?":
                rel_match_b_against_c[rel_idx + cons] = False
                break
    else:
        for i in range(min(C - rel_idx, B)):
            if c[i + rel_idx] != b[i] and c[i + rel_idx] != "?" and b[i] != "?":
                rel_match_b_against_c[rel_idx + cons] = False
                break

for rel_idx in range(-C+1, A):
    if rel_idx < 0:
        for i in range(min(A, rel_idx + C)):
            if a[i] != c[i + rel_idx] and a[i] != "?" and c[i + rel_idx] != "?":
                rel_match_c_against_a[rel_idx + cons] = False
                break
    else:
        for i in range(min(A - rel_idx, C)):
            if a[i + rel_idx] != c[i] and a[i + rel_idx] != "?" and c[i] != "?":
                rel_match_c_against_a[rel_idx + cons] = False
                break

ans = 3*M
for rel_idx_b_against_a in range(-2*M, 2*M):
    for rel_idx_c_against_a in range(-2*M, 2*M):
        if rel_match_b_against_a[rel_idx_b_against_a + cons] and rel_match_c_against_a[rel_idx_c_against_a + cons] and rel_match_b_against_c[rel_idx_b_against_a - rel_idx_c_against_a + cons]:
            ans = min(ans, max(A, B + rel_idx_b_against_a, C + rel_idx_c_against_a) - min(0, rel_idx_b_against_a, rel_idx_c_against_a))

print(ans)