Problem:
Score : 500 points

Problem Statement

Snuke has a string s.
From this string, Anuke, Bnuke, and Cnuke obtained strings a, b, and c, respectively, as follows:

Choose a non-empty (contiguous) substring of s (possibly s itself). Then, replace some characters (possibly all or none) in it with ?s.

For example, if s is mississippi, we can choose the substring ssissip and replace its 1-st and 3-rd characters with ? to obtain ?s?ssip.

You are given the strings a, b, and c.
Find the minimum possible length of s.

Constraints

1 \leq |a|, |b|, |c| \leq 2000

a, b, and c consists of lowercase English letters and ?s.

Input

Input is given from Standard Input in the following format:

a
b
c

Output

Print the minimum possible length of s.

Sample Input 1

a?c
der
cod

Sample Output 1

7

For example, s could be atcoder.

Sample Input 2

atcoder
atcoder
???????

Sample Output 2

7

a, b, and c may not be distinct.

Solution in PyPy3 (2.4.0) (judge verdict: Wrong Answer)

# パナソニック2020E

import sys
input = sys.stdin.readline
sys.setrecursionlimit(max(1000, 10**9))


a,b,c = [input() for _ in range(3)]
la, lb, lc = len(a), len(b), len(c)

def ok(c1,c2):
    return True if c1==c2 or c1=="?" or c2=="?" else False
def sub(s,t,gap=0):
    # tを固定したとき、
    # sの開始位置座標としてありえるもののset
    S = set(list(range(-len(s)-gap, -len(s)+1)) + list(range(len(t), len(t)+gap+1)))
    i1 = min(len(s), len(t))
    i2 = max(len(s), len(t))
    i3 = len(s)+len(t)
    for i in range(1,i1+1):
        if all((ok(s[-i+ii], t[ii])) for ii in range(i)):
            S.add(i-len(s))
    for i in range(i1, i2):
        if len(s)<len(t) and all((ok(s[ii], t[ii+i-i1]) for ii in range(i1))):
            S.add(i-len(s))
        elif len(t)<len(s) and all(ok(s[i2-i+ii], t[ii]) for ii in range(i1)):
            S.add(i-len(s))
    for i in range(i2, i3):
        if all((ok(s[ii], t[i-i3+ii]) for ii in range(i3-i))):
            S.add(i-len(s))
    return S

ab = sub(a,b, lc)
bc = sub(c,b, la)
ac = sub(a,c, lb)
ans = 10**9
import itertools
for ii, jj in itertools.product(ab, bc):
    if ii - jj in ac:
        val = max(lb, ii+la, jj+lc) - min(0, ii, jj)
        ans = min(ans, val)
print(ans)