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.



Sample input:
a?c
der
cod


Sample output:
7

Submission status: Wrong Answer

Solution code:
maxn = 2000 * 3 + 3
maxnh = maxn // 2
 
def check(s, t, d):
    for i in range(max(0, d), min(len(s), len(t) + d)):
        if s[i] != '?' and t[i - d] != '?' and s[i] != t[i - d]:
            return False
    return True
 
def ca(s, t):
    res = [True] * maxn
    for i in range(-len(t), len(s) + 1):
        res[i + maxnh] = check(s, t, i)
    return res
 
a, b, c = (input() for _ in range(3))
ans = sum(map(len, (a, b, c)))
ab, bc, ac = ca(a, b), ca(b, c), ca(a, c)
 
for i in range(maxn):
    if not ab[i]:
        continue
    for j in range(maxn):
        if not bc[j]:
            continue
        if 0 <= i + j - maxnh and i + j - maxnh < maxn and not ac[i + j - maxnh]:
            continue
        ans = min(ans, max(len(a), len(b) + i - maxnh, len(c) + i + j - maxnh * 2) - min(0, i - maxnh, i + j - maxnh * 2))
 
print(ans)

Submission status: Wrong Answer

Solution code:
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 i in bc:
        v = max(len(b),len(c)+i)+len(a)
        mn = min(mn,v)
    return mn

a = ins()
b = ins()
c = ins()
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)

print(mn)

Submission status: Wrong Answer

Solution code:
maxn = 2000 * 3 + 3
maxnh = maxn // 2

def check(s, t, d):
    for i in range(max(0, d), min(len(s), len(t) + d)):
        if s[i] != '?' and t[i - d] != '?' and s[i] != t[i - d]:
            return False
    return True

def ca(s, t):
    res = [True] * maxn
    for i in range(-len(t), len(s) + 1):
        res[i + maxnh] = check(s, t, i)
    return res

a, b, c = (input() for _ in range(3))
ans = sum(map(len, (a, b, c)))
ab, bc, ac = ca(a, b), ca(b, c), ca(a, c)

for i in range(maxn):
    if not ab[i]:
        continue
    for j in range(maxn):
        if not bc[j]:
            continue
        if (0 <= i + j - maxnh < maxn) and not ac[i + j - maxnh]:
            continue
        ans = min(ans, max(len(a), len(b) + i - maxnh, len(c) + i + j - maxnh * 2) - min(0, i - maxnh, i + j - maxnh * 2))

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

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

pair=[a,b,c]

ret=10000

for i1,i2,i3 in itertools.permutations([0,1,2]):
    s1=pair[i1]
    s2=pair[i2]
    s3=pair[i3]

    for s2top in range(len(s1)+1):
        lens12=min(len(s1)-s2top,len(s2))
        
        cnt=sum([0]+[s1[s2top+i]==s2[i] or s1[s2top+i]=='?' or s2[i]=='?' for i in range(lens12)])

        if cnt==lens12:
            s12cs=''.join(['?' if s1[s2top+i]=='?' or s2[i]=='?' else s2[i] for i in range(lens12)])
            s12=s1[:s2top]+s12cs+s1[s2top+len(s12cs):]+s2[lens12:]
            break
    
    for s3top in range(len(s12)+1):
        lens123=min(len(s12)-s3top,len(s3))

        cnt=sum([0]+[s12[s3top+i]==s3[i] or s12[s3top+i]=='?' or s3[i]=='?' for i in range(lens123)])

        if cnt==lens123:
            s123cs=''.join(['?' if s12[s3top+i]=='?' or s3[i]=='?' else s3[i] for i in range(lens123)])
            s123=s12[:s3top]+s123cs+s12[s3top+len(s123cs):]+s3[lens123:]
            break
    
    ret=min(ret,len(s123))

print(ret)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

A = list(input())
B = list(input())
C = list(input())

N = len(A)


def compare(x, y):
    #print("COMPARE:", x, y)
    if not (x and y):
        return False
    for a, b in zip(x, y):
        if a == b or a == '?' or b == '?':
            continue
        else:
            # print("False")
            return False
    # print("True")
    return True


def getNewStr(X, Y):
    ans = []
    for _ in range(2):
        X, Y = Y, X
        for i in range(N, -1, -1):
            if compare(X[-i:], Y[:i]):
                ans.append(X + Y[i:])
                # break

    return ans


def getMaxStr(X, Y):
    #print("getMAX", X, Y)
    n = len(X)
    m = len(Y)
    ans = 0
    for i in range(n, -1, -1):
        if m < i:
            if compare(X[i - m:i], Y):
                ans = max(ans, m)
                break
        else:
            if compare(X[:i], Y[-i:]):
                ans = max(ans, i)
                break

    for i in range(n, -1, -1):
        if m < i:
            if compare(X[-i: -i + m], Y):
                ans = max(ans, m)
                break
        else:
            if compare(X[-i:], Y[i:]):
                ans = max(ans, i)
                break

    return ans


# まずAとBを比較
# 先頭から

ans = N * 3
for _ in range(3):
    A, B, C = C, A, B

    rs = getNewStr(A, B)

    #print("rs", rs)
    if not rs:
        continue

    for r in rs:
        m = getMaxStr(r, C)
        #print("r", r, "m", m)
        if m == 0:
            ans = min(ans, len(r)+len(C))
        elif m == len(C):
            ans = min(ans, len(r))
        else:
            ans = min(ans, len(r)+len(C)-m)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
from itertools import permutations

A = input()
B = input()
C = input()

ans = len(A) + len(B) + len(C)

def merge(X, Y, offset=0, first=False):
  ret = deque()
  for i in range(offset, len(X)):
    s = deque(X[:i])
    for x, y in zip(X[i:], Y):
      if x == '?' or x == y:
        s.append(y)
      elif y == '?':
        s.append(x)
      else:
        break
    else:
      s.extend(Y[len(X)-i:])
      ret.append((list(s), i))
      if first:
        break
  return ret

for X, Y, Z in permutations([A, B, C]):
  for W, i in merge(X, Y):
    if len(W) >= ans:
      break
    ans = min(ans, len(W) + len(Z))
    for V, _ in merge(W, Z, i, True):
      ans = min(ans, len(V))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)


def match(c1, c2):
    return c1 == '?' or c2 == '?' or c1 == c2


def js(s1, s2):

    for i in range(len(s1)):
        flag = True
        for j in range(0, min(len(s1) - i, len(s2))):
            c1 = s1[i + j]
            c2 = s2[j]

            if not match(c1, c2):
                flag = False
                break

        if flag:
            if len(s1) - i >= len(s2):
                l1 = list(s1)
                for j in range(len(s2)):
                    if s1[i + j] == '?':
                        l1[i + j] = s2[j]
                return "".join(l1)
            else:
                l2 = list(s2)
                for j in range(len(s1) - i):
                    if s2[j] == '?':
                        l2[j] = s1[i + j]
                return s1[:i] + "".join(l2)
    return s1 + s2


def main():
    a = input()
    b = input()
    c = input()

    ab = js(a, b)
    ac = js(a, c)
    ba = js(b, a)
    bc = js(b, c)
    ca = js(c, a)
    cb = js(c, b)

    abc = js(ab, c)
    acb = js(ac, b)
    bac = js(ba, c)
    bca = js(bc, a)
    cab = js(ca, b)
    cba = js(cb, a)

    min_l = min([len(s) for s in (abc, acb, bac, bca, cab, cba)])
    print(min_l)


main()

Submission status: Wrong Answer

Solution code:
import itertools

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

pair=[a,b,c]

ret=10000

for i1,i2,i3 in itertools.permutations([0,1,2]):
    s1=pair[i1]
    s2=pair[i2]
    s3=pair[i3]

    for s2top in range(len(s1)+1):
        lens12=min(len(s1)-s2top,len(s2))
        
        cnt=sum([0]+[s1[s2top+i]==s2[i] or s1[s2top+i]=='?' or s2[i]=='?' for i in range(lens12)])

        if cnt==lens12:
            s12cs=''.join(['?' if s1[s2top+i]=='?' or s2[i]=='?' else s2[i] for i in range(lens12)])
            s12=s1[:s2top]+s12cs+s2[lens12:]
            break
    
    for s3top in range(len(s12)+1):
        lens123=min(len(s12)-s3top,len(s3))

        cnt=sum([0]+[s12[s3top+i]==s3[i] or s12[s3top+i]=='?' or s3[i]=='?' for i in range(lens123)])

        if cnt==lens123:
            s123cs=''.join(['?' if s12[s3top+i]=='?' or s3[i]=='?' else s3[i] for i in range(lens123)])
            s123=s12[:s3top]+s123cs+s3[lens123:]
            break
    
    ret=min(ret,len(s123))

print(ret)

Submission status: Wrong Answer

Solution code:
maxn = 2000 * 3 + 3
maxnh = maxn // 2

def check(s, t, d):
    for i in range(max(0, d), min(len(s), len(t) + d)):
        if s[i] != '?' and t[i - d] != '?' and s[i] != t[i - d]:
            return False
    return True

def ca(s, t):
    res = [True] * maxn
    for i in range(-len(t), len(s) + 1):
        res[i + maxnh] = check(s, t, i)
    return res

a, b, c = (input() for _ in range(3))
ans = sum(map(len, (a, b, c)))
ab, bc, ac = ca(a, b), ca(b, c), ca(a, c)
for i in range(maxn):
    if not ab[i]:
        continue
    for j in range(maxn):
        if not bc[j]:
            continue
        if 0 <= i + j - maxnh < maxn and not ac[i + j - maxnh]:
            continue
        ans = min(ans, max(len(a), len(b) + i - maxnh, len(c) + i + j - maxnh * 2) - min(0, i - maxnh, i + j - maxnh * 2))
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools, copy

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

s = [a, b, c]
ans = 6000
for i in itertools.permutations(s):
    check = [None] * 6000
    temp1 = list(i[0])
    for j in range(len(temp1)):
        check[j] = temp1[j]
    for j in range(len(temp1) + 1):
        flag = True
        temp2 = list(i[1])
        check2 = copy.copy(check)
        for k in range(len(temp2)):
            if check2[j + k] == None or check2[j + k] == "?":
                check2[j + k] = temp2[k]
            elif check2[j + k] != temp2[k]:
                flag = False
                break
        if flag == False:
            continue
        else:
            for k in range(max(len(temp1), j + len(temp2)) + 1):
                temp3 = list(i[2])
                flag2 = True
                for l in range(len(temp3)):
                    if temp3[l] == "?":
                        continue
                    else:
                        if check2[k + l] == None or check2[k + l] == "?":
                            continue
                        elif check2[k + l] == temp3[l]:
                            continue
                        else:
                            flag2 = False
                            break
                if flag2 == False:
                    continue
                else:
                    ans = min(ans, max(len(temp1), j + len(temp2), k + len(temp3)))
print(ans)

Submission status: Wrong Answer

Solution code:
a = input()
b = input()
c = input()
p = len(a)
q = len(b)
r = len(c)


def func(a, b):
    p = len(a)
    q = len(b)
    for i in range(p):
        f = 0
        z = min(q, p - i)
        for j in range(z):
            if a[j + i] != "?" and b[j] != "?" and a[j + i] != b[j]:
                break
            f += 1
        if f == z:
            res = []
            for j in range(i):
                res.append(a[j])
            if p > q + i:
                for j in range(q):
                    if a[i + j] == "?":
                        res.append(b[j])
                    else:
                        res.append(a[i + j])
                for j in range(q + i, p):
                    res.append(a[j])
            elif p == q + i:
                for j in range(q):
                    if a[i + j] == "?":
                        res.append(b[j])
                    else:
                        res.append(a[i + j])
            else:
                for j in range(p - i):
                    if a[i + j] == "?":
                        res.append(b[j])
                    else:
                        res.append(a[i + j])
                for j in range(p - i, q):
                    res.append(b[j])
            return "".join(map(str, res))
    return a + b


res = []
res.append(func(func(a, b), c))
res.append(func(func(a, c), b))
res.append(func(func(b, a), c))
res.append(func(func(b, c), a))
res.append(func(func(c, a), b))
res.append(func(func(c, b), a))

result = 10 ** 8
for sres in res:
    result = min(result, len(sres))
print(result)

Submission status: Wrong Answer

Solution code:
maxn = 2000 * 3 + 3
maxnh = maxn // 2

def check(s, t, d):
    for i in range(max(0, d), min(len(s), len(t) + d)):
        if s[i] != '?' and t[i - d] != '?' and s[i] != t[i - d]:
            return False
    return True

def ca(s, t):
    res = [True] * maxn
    for i in range(-len(t), len(s) + 1):
        res[i + maxnh] = check(s, t, i)
    return res

a, b, c = (input() for _ in range(3))
ans = sum(map(len, (a, b, c)))
ab, bc, ac = ca(a, b), ca(b, c), ca(a, c)
for i in range(maxn):
    if not ab[i]:
        continue
    for j in range(maxn):
        if not bc[j]:
            continue
        if not (0 <= i + j - maxnh < maxn) or not ac[i + j - maxnh]:
            continue
        ans = min(ans, max(len(a), len(b) + i - maxnh, len(c) + i + j - maxnh * 2) - min(0, i - maxnh, i + j - maxnh * 2))
print(ans)

Submission status: Wrong Answer

Solution code:
a = input()
b = input()
c = input()

def match(c1, c2):
    return c1 == c2 or c1 == "?" or c2 == "?"

def check(x, y, l_m=2000):
    xy = [0] * (l_m * 8 + 1)
    mid = l_m * 2
    for i in range(len(x)):
        for j in range(len(y)):
            if not match(x[i], y[j]):
                xy[i - j + mid] = 1
    return xy

l_a = len(a)
l_b = len(b)
l_c = len(c)

ab = check(a, b)
ac = check(a, c)
bc = check(b, c)

l_m = 2000
mid = l_m * 2

ans = 3 * l_m
for i in range(-2 * l_m, 2 * l_m + 1):
    if ab[i + l_m]:
        continue
    for j in range(-2 * l_m, 2 * l_m + 1):
        if ac[j+mid] or bc[j-i+mid]:
            continue

        lb = min(0, min(i, j))
        ub = max(l_a, max(l_b + i, l_c + j))
        ans = min(ans, ub - lb)
            
print(ans)

Submission status: Wrong Answer

Solution code:
a = input()
b = input()
c = input()
p = len(a)
q = len(b)
r = len(c)


def func(a, b):
    p = len(a)
    q = len(b)
    for i in range(p):
        f = 0
        z = min(q, p - i)
        for j in range(z):
            if a[j + i] != "?" and b[j] != "?" and a[j + i] != b[j]:
                break
            f += 1
        if f == z:
            res = []
            for j in range(i):
                res.append(a[j])
            if p > q + i:
                for j in range(q):
                    if a[i + j] == "?":
                        res.append(b[j])
                    else:
                        res.append(a[i + j])
                for j in range(q + i, p):
                    res.append(a[j])
            elif p == q + i:
                for j in range(q):
                    if a[i + j] == "?":
                        res.append(b[j])
                    else:
                        res.append(a[i + j])
            else:
                for j in range(p - i):
                    if a[i + j] == "?":
                        res.append(b[j])
                    else:
                        res.append(a[i + j])
                for j in range(p - i, q):
                    res.append(b[j])
            return "".join(map(str, res))
    return a + b


res = []
res.append(func(func(a, b), c))
res.append(func(func(a, c), b))
res.append(func(func(b, a), c))
res.append(func(func(b, c), a))
res.append(func(func(c, a), b))
res.append(func(func(c, b), a))

result = 10 * 8
for sres in res:
    result = min(result, len(sres))
print(result)

Submission status: Wrong Answer

Solution code:
import itertools
a = list(input())
b = list(input())
c = list(input())
w = [a, b, c]

ans = 10000

for x in itertools.permutations(range(3), 3):
    l0 = len(w[x[0]])
    l1 = len(w[x[1]])
    l2 = len(w[x[2]])

    org1 = [-1]*(l0+l1+l2)
    for k in range(l0):
        org1[k] = w[x[0]][k]

    for i in range(l0+1):
        org = org1[:]
        error = False
        for k in range(l0):
            org[k] = w[x[0]][k]
        for k in range(l1):
            if org[i+k] == '?' or org[i+k] == -1 or org[i+k] == w[x[1]][k]:
                org[i+k] = w[x[1]][k]
            else:
                error = True
                break
        if error:
            continue
        
        org2 = org[:]
        for j in range(l1+1):
            error = False
            org = org2[:]
            for k in range(l2):
                if org[i+j+k] == '?' or org[i+j+k] == -1 or org[i+j+k] == w[x[2]][k]:
                    org[i+j+k] = w[x[2]][k]
                else:
                    error = True
                    break
            if not error:
                if -1 in org:
                    tmp = org.index(-1)
                else:
                    tmp = len(org)
                ans = min(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
from itertools import permutations

A = input()
B = input()
C = input()

ans = len(A) + len(B) + len(C)

def merge(X, Y, offset=0):
  ret = deque()
  for i in range(offset, len(X)):
    s = deque(X[:i])
    for x, y in zip(X[i:], Y):
      if x == '?' or x == y:
        s.append(y)
      elif y == '?':
        s.append(x)
      else:
        break
    else:
      s.extend(Y[len(X)-i:])
      ret.append((list(s), i))
  return ret

for X, Y, Z in permutations([A, B, C]):
  for W, i in merge(X, Y):
    if len(W) >= ans:
      break
    ans = min(ans, len(W) + len(Z))
    for V, _ in merge(W, Z, i):
      ans = min(ans, len(V))

print(ans)

Submission status: Wrong Answer

Solution code:
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,stt):
    l = []
    for i in range(stt,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 cmp(a,b):
    l1 = cmp1(a,b,0)
    l2 = cmp1(b,a,0)
    return (l1,l2)

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 = (ab[1],ab[0])
ca = (ac[1],ac[0])
cb = (bc[1],bc[0])
mn = la+lb+lc
for p,q in [(0,1),(1,0)]:
  for ii in ab[p]:
    i = ii if p==0 else -ii
    for jj in ac[q]:
        j = jj if q==0 else -jj
        if lc+j<=i or lb+i<=j or (j-i) in bc[0] or (i-j) in bc[1]:
            v = max([la,lb+i,lc+j]) + max(0,-i,-j)
            mn = min(mn,v)
ddprint(mn)
for p,q in [(0,1),(1,0)]:
  for ii in ba[p]:
    i = ii if p==0 else -ii
    for jj in bc[q]:
        j = jj if q==0 else -jj
        if lc+j<=i or la+i<=j or (j-i) in ac[0] or (i-j) in ac[1]:
            v = max([la+i,lb,lc+j]) + max(0,-i,-j)
            mn = min(mn,v)
ddprint(mn)
for p,q in [(0,1),(1,0)]:
  for ii in ca[p]:
    i = ii if p==0 else -ii
    for jj in cb[q]:
        j = jj if q==0 else -jj
        if lb+j<=i or la+i<=j or (j-i) in ab[0] or (i-j) in ab[1]:
            v = max([la+i,lb+j,lc]) + max(0,-i,-j)
            mn = min(mn,v)
ddprint(mn)
for p in [0,1]:
 for i in ab[p]:
    i = ii if p==0 else -ii
    v = max(la,lb+i)+max(0,-i)+lc
    mn = min(mn,v)
ddprint(mn)
for p in [0,1]:
 for i in ac[p]:
    i = ii if p==0 else -ii
    v = max(la,lc+i)+max(0,-i)+lb
    mn = min(mn,v)
ddprint(mn)
for p in [0,1]:
 for i in bc[p]:
    i = ii if p==0 else -ii
    v = max(lb,lc+i)+max(0,-i)+la
    mn = min(mn,v)
print(mn)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)


def js(s1, s2):

    for i in range(len(s1)):
        flag = True
        for j in range(0, min(len(s1) - i, len(s2))):
            c1 = s1[i + j]
            c2 = s2[j]

            if c1 != '?' and c2 != '?' and c1 != c2:
                flag = False
                break

        if flag:
            if len(s1) >= len(s2) + i:
                l1 = list(s1)
                for j in range(len(s2)):
                    if s1[i + j] == '?':
                        l1[i+j] = s2[j]
                return "".join(l1)
            else:
                l2 = list(s2)
                for j in range(len(s1) - i):
                    if s2[j] == '?':
                        l2[j] = s1[i + j]
                return s1[:i] + "".join(l2)
    return s1 + s2


def main():
    a = input()
    b = input()
    c = input()

    ab = js(a, b)
    ac = js(a, c)
    ba = js(b, a)
    bc = js(b, c)
    ca = js(c, a)
    cb = js(c, b)

    abc = js(ab, c)
    acb = js(ac, b)
    bac = js(ba, c)
    bca = js(bc, a)
    cab = js(ca, b)
    cba = js(cb, a)

    print(min(len(s) for s in (abc, acb, bac, bca, cab, cba)))


main()

Submission status: Wrong Answer

Solution code:
from collections import deque
from itertools import permutations

A = input()
B = input()
C = input()

ans = len(A) + len(B) + len(C)

def merge(X, Y, offset=0):
  ret = deque()
  for i in range(offset, len(X)):
    s = deque(X[:i])
    for x, y in zip(X[i:], Y):
      if x == '?' or x == y:
        s.append(y)
      elif y == '?':
        s.append(x)
      else:
        break
    else:
      s.extend(Y[len(X)-i:])
      ret.append((list(s), i))
  return ret

for X, Y, Z in permutations([A, B, C]):
  for W, i in merge(X, Y):
    if len(W) >= ans:
      break
    ans = min(ans, len(W) + len(Z))
    for E in merge(W, Z, i):
      ans = min(ans, len(E))

print(ans)

Submission status: Wrong Answer

Solution code:
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,stt):
    l = []
    for i in range(stt,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 cmp(a,b):
    l1 = cmp1(a,b,0)
    l2 = cmp1(b,a,1)
    return (l1,l2)

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 = (ab[1],ab[0])
ca = (ac[1],ac[0])
cb = (bc[1],bc[0])
mn = la+lb+lc
for p,q in [(0,1),(1,0)]:
  for ii in ab[p]:
    i = ii if p==0 else -ii
    for jj in ac[q]:
        j = jj if q==0 else -jj
        if lc+j<=i or lb+i<=j or (j-i) in bc[0] or (i-j) in bc[1]:
            v = max([la,lb+i,lc+j]) + max(0,-i,-j)
            mn = min(mn,v)
ddprint(mn)
for p,q in [(0,1),(1,0)]:
  for ii in ba[p]:
    i = ii if p==0 else -ii
    for jj in bc[q]:
        j = jj if q==0 else -jj
        if lc+j<=i or la+i<=j or (j-i) in ac[0] or (i-j) in ac[1]:
            v = max([la+i,lb,lc+j]) + max(0,-i,-j)
            mn = min(mn,v)
ddprint(mn)
for p,q in [(0,1),(1,0)]:
  for ii in ca[p]:
    i = ii if p==0 else -ii
    for jj in cb[q]:
        j = jj if q==0 else -jj
        if lb+j<=i or la+i<=j or (j-i) in ab[0] or (i-j) in ab[1]:
            v = max([la+i,lb+j,lc]) + max(0,-i,-j)
            mn = min(mn,v)
ddprint(mn)
for p in [0,1]:
 for i in ab[p]:
    i = ii if p==0 else -ii
    v = max(la,lb+i)+max(0,-i)+lc
    mn = min(mn,v)
ddprint(mn)
for p in [0,1]:
 for i in ac[p]:
    i = ii if p==0 else -ii
    v = max(la,lc+i)+max(0,-i)+lb
    mn = min(mn,v)
ddprint(mn)
for p in [0,1]:
 for i in bc[p]:
    i = ii if p==0 else -ii
    v = max(lb,lc+i)+max(0,-i)+la
    mn = min(mn,v)
print(mn)

Submission status: Wrong Answer

Solution code:
def comp(a,b):
  r=""
  for x,y in zip(a,b):
    if x==y:
      r += x
    elif y=="?":
      r += x
    elif x=="?":
      r += y
    else:
      return False
  return r

def min_merge(a,b):
  a_b=[]
  for i in range(len(a)-len(b)+1):
    m = comp(a[i:],b)
    if m:
      a_b.append(a[:i]+m+a[i+len(b):])
  
  if not a_b:
    for i in range(1,len(b)):
      m = comp(b[i:], a)
      if m:
        a_b.append(b[:i]+m+a[len(b)-i:])
      m = comp(b, a[-len(b)+i:])
      if m:
        a_b.append(a[:-len(b)+i]+m+b[-i:])
      if a_b:
        break
  if not a_b:
    a_b.append(a+b)
    a_b.append(b+a)
  return a_b

a=input()
b=input()
c=input()
a,b,c=sorted([a,b,c],key=len,reverse=True)
a_b=min_merge(a,b)
a_b_c = min(len(min_merge(d, c)[0]) for d in a_b)

a_c=min_merge(a,c)
a_c_b = min(len(min_merge(d, b)[0]) for d in a_c)

b_c=min_merge(b,c)
if len(a) < len(b_c[0]):
  b_c_a = min(len(min_merge(d, a)[0]) for d in b_c)
else:
  b_c_a = min(len(min_merge(a, d)[0]) for d in b_c)

print(min(a_b_c, a_c_b, b_c_a))

Submission status: Wrong Answer

Solution code:
def comp(a,b):
  r=""
  for x,y in zip(a,b):
    if x==y:
      r += x
    elif y=="?":
      r += x
    elif x=="?":
      r += y
    else:
      return False
  return r

def min_merge(a,b):
  a_b=[]
  for i in range(len(a)-len(b)+1):
    m = comp(a[i:],b)
    if m:
      a_b.append(a[:i]+m+a[i+len(b):])
  
  if not a_b:
    for i in range(1,len(b)):
      m = comp(b[i:], a)
      if m:
        a_b.append(b[:i]+m+a[len(b)-i:])
      m = comp(b, a[-len(b)+i:])
      if m:
        a_b.append(a[:-len(b)+i]+m+b[-i:])
      if a_b:
        break
  if not a_b:
    a_b.append(a+b)
    a_b.append(b+a)
  return a_b

a="atcoder"
b="atco"
c="??d????"
a,b,c=sorted([a,b,c],key=len,reverse=True)
a_b=min_merge(a,b)
a_b_c = min(len(min_merge(d, c)[0]) for d in a_b)

a_c=min_merge(a,c)
a_c_b = min(len(min_merge(d, b)[0]) for d in a_c)

b_c=min_merge(b,c)
if len(a) < len(b_c[0]):
  b_c_a = min(len(min_merge(d, a)[0]) for d in b_c)
else:
  b_c_a = min(len(min_merge(a, d)[0]) for d in b_c)

print(min(a_b_c, a_c_b, b_c_a))

Submission status: Wrong Answer

Solution code:
def comp(a,b):
  r=""
  for x,y in zip(a,b):
    if x==y:
      r += x
    elif y=="?":
      r += x
    elif x=="?":
      r += y
    else:
      return False
  return r

def min_merge(a,b):
  a_b=[]
  for i in range(len(a)-len(b)+1):
    m = comp(a[i:],b)
    if m:
      a_b.append(a[:i]+m+a[i+len(b):])
  
  if not a_b:
    for i in range(1,len(b)):
      m = comp(b[i:], a)
      if m:
        a_b.append(b[:i]+m+a[len(b)-i:])
      m = comp(b, a[-len(b)+i:])
      if m:
        a_b.append(a[:-len(b)+i]+m+b[-i:])
      if a_b:
        break
  if not a_b:
    a_b.append(a+b)
    a_b.append(b+a)
  return a_b

a="atcoder"
b="atcoder"
c="???????"
a,b,c=sorted([a,b,c],key=len,reverse=True)
a_b=min_merge(a,b)
a_b_c = min(len(min_merge(d, c)[0]) for d in a_b)

a_c=min_merge(a,c)
a_c_b = min(len(min_merge(d, c)[0]) for d in a_c)

b_c=min_merge(b,c)
if len(a) < len(b_c[0]):
  b_c_a = min(len(min_merge(d, a)[0]) for d in b_c)
else:
  b_c_a = min(len(min_merge(a, d)[0]) for d in b_c)

print(min(a_b_c, a_c_b, b_c_a))

Submission status: Wrong Answer

Solution code:
from collections import deque
from itertools import permutations

A = input()
B = input()
C = input()

ans = len(A) + len(B) + len(C)

def merge(X, Y, offset=0):
  ret = deque()
  for i in range(offset, len(X)):
    s = deque(X[:i])
    for x, y in zip(X[i:], Y):
      if x == '?' or x == y:
        s.append(y)
      elif y == '?':
        s.append(x)
      else:
        break
    else:
      s.extend(Y[len(X)-i:])
      ret.append(list(s))
  return ret

for X, Y, Z in permutations([A, B, C]):
  for W in merge(X, Y):
    if len(W) >= ans:
      break
    ans = min(ans, len(W) + len(Z))
    for E in merge(W, Z, len(W) - len(Y)):
      ans = min(ans, len(E))

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import permutations
def match(a,b):
    nb = len(b)
    na = len(a)
    n=min(nb,na)
    res=[0]
    for i in range(1,n+1):
        for j in range(i):
            if not(a[na-i+j] == b[j] or a[na-i+j] == '?' or  b[j] == '?'):
                break
        else:
            res.append(i)
    return res


a = input()
b = input()
c = input()
stls = [a,b,c]
ans=1001001001

for ls in permutations(stls):
    ansstring = ls[0]
    idx = match(ansstring, ls[1])
    for i in idx:
        ansstring2 = ansstring+ls[1][i:]
        idx2 = match(ansstring2, ls[2])
        ansstring2 += ls[2][idx2[-1]:]
        ans=min(ans,len(ansstring2))

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 14 23:05:24 2020

@author: Kanaru Sato
"""

def rdndnt(a,b):
    la = len(a)
    lb = len(b)
    l = min(la,lb)
    n = 0
    for i in range(l):
        if a[la-1-i] != "?" and b[i] != "?":
            if a[la-1-i:la] == b[0:i+1]:
                n = i+1
            else:
                break
        else:
            n = i+1
    return n
            
s = input()
t = input()
u = input()

st = rdndnt(s,t)
ts = rdndnt(t,s)
tu = rdndnt(t,u)
ut = rdndnt(u,t)
us = rdndnt(u,s)
su = rdndnt(s,u)

stu = st+tu
sut = su+ut
tsu = ts+su
tus = tu+us
uts = ut+ts
ust = us+st

M = max(stu,sut,tsu,tus,uts,ust)
ans = len(s)+len(t)+len(u)-M

print(ans)