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:
a=list(input())
b=list(input())
c=list(input())
X=[a,b,c]

def mix(s,t):
    if len(s)<len(t):
        s=["?"]*(len(t)-len(s))+s
    for length in range(len(t),0,-1):
        u=s[-length:]
        v=t[:length]

        #print(u,v)

        for i in range(length):
            if u[i]==v[i] or u[i]=="?" or v[i]=="?":
                continue
            else:
                break
        else:
            ret=[]
            for i in range(length):
                if u[i]=="?":
                    ret+=v[i]
                else:
                    ret+=u[i]
                
            yield s[:-length]+ret+t[length:]

    yield s+t

from itertools import permutations
ITE=permutations([0,1,2])
ANS=[]
for i,j,k in ITE:
    for l in mix(X[i],X[j]):
        ANS.extend(list(mix(l,X[k])))
        ANS.extend(list(mix(X[k],l)))

L=1<<30
for a in ANS:
    L=min(L,len(a))
print(L)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8
import sys
sys.setrecursionlimit(10000000)

#const
# my functions here!
def pin(type=int):
    return map(type,input().rstrip().split())
"""
def resolve():
    a,b,c=pin()
    from math import sqrt
    cond =(c-a-b)>sqrt(a*b)*2
    print(["No","Yes"][cond])
"""
def resolve():
    t=[input() for i in range(3)]
    ans=sum(map(lambda x:len(x),t))#デフォ値
    
    from itertools import permutations as p
    for perm in p(range(3)):
        x,y,z=t[perm[0]],t[perm[1]],t[perm[2]]
        X,Y,Z=len(x),len(y),len(z)
        kasa=[0,0]
        
        #xy
        
        for ixy in range(min(X,Y),-1,-1):
            #ixyはxのケツとyの頭での文字の重なり（?をふくむ
            #重なりの最大を求めたいのでrev
            #print("ya")
            #ixy個の文字比較
            flag=0
            for pxy in range(ixy):
               # print(x,y,ixy)
                #print(y[pxy],x[X-ixy+pxy],(y[pxy]==x[X-ixy+pxy]))
                if y[pxy]!="?" and x[X-ixy+pxy]!="?":
                    if y[pxy]!=x[X-ixy+pxy]:
                        flag=1
                        break
            if flag==1:continue
            if flag==0:
               # print("end")
                kasa[0]=ixy
                break
            flag=0
        #yz       
        
        for iyz in range(min(Z,Y),-1,-1):
            #ixyはxのケツとyの頭での文字の重なり（?をふくむ
            #重なりの最大を求めたいのでrev
            
            #ixy個の文字比較
            flag2=0
            for pyz in range(iyz):
                if z[pyz]!="?" and y[Y-iyz+pyz]!="?":
                    if z[pyz]!=y[Y-iyz+pyz]:
                        flag2=1
                        break
            if flag==1:continue
            if flag2==0:
                kasa[1]=iyz
                break
            flag2=0
        #print(kasa)
        ans=min(ans,X+Y+Z-sum(kasa))
    print(ans)
"""         
#printデバッグ消した？
#前の問題の結果見てないのに次の問題に行くの？
"""
"""
お前カッコ閉じるの忘れてるだろ
"""
import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
        
    def test_入力例_1(self):
        input = """a?c
der
cod"""
        output = """7"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """atcoder
atcoder
???????"""
        output = """7"""
        self.assertIO(input, output)

if __name__=="__main__":resolve()

Submission status: Wrong Answer

Solution code:
def conbine(a,b):
    result=a+b
    for i in range(len(a)):
        flag=True
        tmp=a[:i]
        for j in range(len(b)):
            if len(a)<=i+j:
                tmp+=b[j:]
                break
            if not (a[i+j]==b[j] or a[i+j]=='?' or b[j]=='?'):
                flag=False
                break
            
            if a[i+j]=='?':
                tmp+=b[j]
            else:
                tmp+=a[i+j]

        tmp+=a[len(tmp)+1:]

        if flag:
            result=tmp if len(tmp)<len(result) else result
    
    return result

def main():
    import sys
    import math
    dic={}
    dic['a']=sys.stdin.readline().strip()
    dic['b']=sys.stdin.readline().strip()
    dic['c']=sys.stdin.readline().strip()

    result=7000

    abc=['a','b','c']
    for alp1 in abc:
        for alp2 in abc:
            if alp1==alp2:
                continue
            for alp3 in abc:
                if alp3==alp2 or alp3==alp1:
                    continue

                ts=conbine(dic[alp1],dic[alp2])
                s=conbine(ts,dic[alp3])
                result=len(s) if len(s) < result else result
                return result


    return result

print(main())

Submission status: Wrong Answer

Solution code:
def check(a,b,c):
    ma = 0
    ml = min(len(a),len(b))
    for i in range(ml):
        f = False
        for j in range(ml-i):
            # print('pair', a[i+j], b[ml-1-i-j])
            if (a[-j-1] == b[ml-1-i-j]) or a[-1-j] == '?' or b[ml-1-i-j] == '?':
                # print(a[i+j], b[ml-1-i-j])
                continue
            else:
                f = True
                break
        if not f:
            ma = ml -i
            break
    
    for i in range(ma):
        if b[ma-1-i] == '?' and a[-1-i] != '?':
            b = list(b)
            b[ma-1-i] = a[-1-i]
            b = ''.join(b)
        
    mb = 0
    ml = min(len(b),len(c))
    for i in range(ml):
        f = False
        for j in range(ml-i):
            if (b[-j-1] == c[ml-1-i-j]) or b[-1-j] == '?' or c[ml-1-i-j] == '?':
                continue
            else:
                f = True
                break
        if not f:
            mb = ml -i
            break
    # print(len(a)-ma , len(c) - mb , len(b))
    return len(a)-ma + len(c) - mb + len(b)

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

s = float('inf')
s = min(s, check(a,b,c))
s = min(s, check(a,c,b))
s = min(s, check(b,c,a))
s = min(s, check(b,a,c))
s = min(s, check(c,b,a))
s = min(s, check(c,a,b))
print(s)

Submission status: Wrong Answer

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

s = [a, b, c]
le = [len(a), len(b), len(c)]

def check2(s1, s2) :
    ln = len(s1) + len(s2) - 1
    s1 = "?"*(len(s2)-1) + s1 + "?"*(len(s2)-1)
    ans = 0
    for i in range(ln) :
        flg = True
        for j in range(min(len(s1),len(s2))) :
            if s1[i+j] != s2[j] and s1[i+j] != "?" and s2[j] != "?" :
                    flg = False
        if flg == True :
            if len(s2) <= i <= ln - len(s2) :
                ans = max(ans, min(len(s1),len(s2)))
            elif i < len(s2) :
                ans = max(ans, i+1)
            else :
                ans = max(ans, ln - i + 1)
    return ans

s.sort(key=len, reverse = True)
tmp = check2(s[0], s[1]) + check2(s[0], s[2])
print(sum(le) - tmp)

Submission status: Wrong Answer

Solution code:
from itertools import permutations

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


def make_chain(a, b, i):
    chain = a[:i]
    for j in range(len(b)):
        if len(a) <= i + j:
            break
        if a[i + j] == b[j]:
            chain += a[i + j]
            continue
        if a[i + j] == "?":
            chain += b[j]
            continue
        if b[j] == "?":
            chain += a[i + j]
            continue
        return ""
    if i + len(b) > len(a):
        chain += b[len(a) - i:]
    else:
        chain += a[i + len(b): ]
    return chain


def kmp(s1, s2):
    s = "%s!%s" % (s1, s2)
    p = [0] * len(s)
    for i in range(1, len(s)):
        k = p[i - 1]
        while k > 0 and s[k] != s[i]:
            k = p[k - 1]
        if s[k] == s[i] or s[k] == '?' or s[i] == '?':
            k += 1
        p[i] = k
    return len(s1) - p[-1] + len(s2)

best_size = len(A) + len(B) + len(C)
for a, b, c in permutations([A, B, C]):
    for i in range(len(a) + 1):
        chain = make_chain(a, b, i)
        if chain:
            size = kmp(chain, c)
            if size:
                best_size = min(best_size, size)
print(best_size)

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

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

all =[
    [a,b,c],
    [a,c,b],
    [b,a,c],
    [b,c,a],
    [c,a,b],
    [c,b,a]
]

def check_dig(str_a, str_b):
    double_nums = 0
    for i in range(1,min(len(str_a), len(str_b)) +1):
        part_a = str_a[-i:]
        part_b = str_b[:i]
        for j in range(len(part_a)):
            if part_a[j] == "?" or part_b[j] == "?" or part_b[j] == part_a[j]:
                pass
            else:
                break
        else:
            double_nums = len(part_a)
    # print(str_a, str_b)
    # print(double_nums)
    return double_nums

ans = len(a) + len(b) + len(c)
cnt = -1
for orders in all:
    tmp = 0
    for i in range(2):
        tmp += check_dig(orders[i], orders[i + 1])
    cnt = max(cnt, tmp)

ans -= cnt
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
#Z[i]:length of the longest list starting from S[i] which is also a prefix of S
#O(|S|)
def Z_algorithm(s,L):
    N = len(s)
    Z_alg = [0]*N

    Z_alg[0] = N
    i = L
    j = 0
    while i < N:
        while i+j < N and (s[j] == s[i+j] or s[j] == "?" or s[i+j] == "?"):
            j += 1
        Z_alg[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while i+k < N and k + Z_alg[k]<j:
            Z_alg[i+k] = Z_alg[k]
            k += 1
        i += k
        j -= k
    return Z_alg

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


shaaa = ["$"]*2001
def de(x):
    n = len(x)
    for i in range(n):
        if x[i] == "$":
            return x[:i] 
    return x

def unite(x,y):
    y = y + shaaa
    X,Y = len(x),len(y)
    z = Z_algorithm(y+x,Y)

    tmp = -1
    for i in range(Y,X+Y):
        if z[i]+i == X+Y or z[i] == Y:
            tmp = i
            break


    if tmp == -1:
        return de(x+y)
    else:
        if z[tmp] == Y:
            res = x
            for i in range(tmp-Y,tmp):
                if res[i+Y-tmp] == "?":
                    res[i] = y[i-(tmp-Y)]
            return de(res)
        else:
            res = x
            for i in range(tmp-Y,X):
                if res[i] == "?":
                    res[i] = y[i-(tmp-Y)]
            for i in range(X-tmp+Y,Y):
                res.append(y[i])
            return de(res)

print(min(
len(unite(unite(a,b),c)),
len(unite(unite(a,c),b)),
len(unite(unite(b,a),c)),
len(unite(unite(b,c),a)),
len(unite(unite(c,a),b)),
len(unite(unite(c,b),a))))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
#input = sys.stdin.readline
def inpl(): return list(map(int, input().split()))
A = input()
B = input()
C = input()


ab = []
bc = []
ca = []

res = len(A) + len(B) + len(C)
for i in range(len(A)+len(B)-1):
    X = A[min(max(0, len(B) - i -1), len(A)):min(len(A)+len(B)-2, len(A)+len(B)-i-1)]
    Y = B[max(0, i-len(A)+1):i+1]
    for x, y in zip(X, Y):
        if (x=="?") or (y=="?") or (x==y):
            continue
        else:
            break
        
    else:
        ab.append(i)
        res = min(res, len(A)+len(B)+len(C)-len(X))

for i in range(len(B)+len(C)-1):
    X = B[min(max(0, len(C) - i -1), len(B)):min(len(B)+len(C)-2, len(B)+len(C)-i-1)]
    Y = C[max(0, i-len(B)+1):i+1]
    for x, y in zip(X, Y):
        if (x=="?" or y=="?" or x==y):
            continue
        else:
            break
    else:
        bc.append(i)
        res = min(res, len(A)+len(B)+len(C)-len(X))

for i in range(len(C)+len(A)-1):
    X = C[min(max(0, len(A) - i -1), len(C)):min(len(C)+len(A)-2, len(C)+len(A)-i-1)]
    Y = A[max(0, i-len(C)+1):i+1]
    for x, y in zip(X, Y):
        if (x=="?" or y=="?" or x==y):
            continue
        else:
            break
    else:
        ca.append(i)
        res = min(res, len(A)+len(B)+len(C)-len(X))
ab = set(ab)
bc = set(bc)
ca = set(ca)
for x in ab:
    for y in bc:
        z = x + y - len(B) + 1
        if z<0 or (len(C)+len(A)-1 <= z) or (z in ca):
            #print(len(B) + max(0, len(A)-x-1, y-len(B)+1) + max(0, x-len(B)+1, len(C)-y-1))
            res = min(res, len(B) + max(0, len(A)-x-1, y-len(B)+1) + max(0, x-len(B)+1, len(C)-y-1))
for x in bc:
    for y in ca:
        z = x + y - len(C) + 1
        if z<0 or (len(A)+len(B)-1 <= z) or (z in ab):
            #print(len(B) + max(0, len(A)-x-1, y-len(B)+1) + max(0, x-len(B)+1, len(C)-y-1))
            res = min(res, len(C) + max(0, len(B)-x-1, y-len(C)+1) + max(0, x-len(C)+1, len(A)-y-1))
for x in ca:
    for y in ab:
        z = x + y - len(A) + 1
        if z<0 or (len(B)+len(C)-1 <= z) or (z in bc):
            #print(len(B) + max(0, len(A)-x-1, y-len(B)+1) + max(0, x-len(B)+1, len(C)-y-1))
            res = min(res, len(A) + max(0, len(C)-x-1, y-len(A)+1) + max(0, x-len(A)+1, len(B)-y-1))
print(res)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(a: str, b: str, c: str):
    a, b, c = sorted([a, b, c], key=lambda x: -len(x))
    a = list(a)
    b = list(b)
    c = list(c)

    def partial_match_table(word):
        table = [0] * (len(word) + 1)
        table[0] = -1
        i, j = 0, 1
        while j < len(word):
            matched = word[i] == word[j] or word[i] == "?" or word[j] == "?"
            if not matched and i > 0:
                i = table[i]
            else:
                if matched:
                    i += 1
                j += 1
                table[j] = i
        return table

    res = []

    def kmp_search(a, b, c):
        pad = len(b)
        text = ["?"] * pad + a + ["?"] * pad
        table = partial_match_table(b)
        i, p = 0, 0
        while i < len(text) and p < len(b):
            if text[i] == b[p] or text[i] == "?" or b[p] == "?":
                i += 1
                p += 1
                if p == len(b):
                    idx = i - p

                    ppad = len(c)
                    if idx < ppad:
                        plane = b + a[len(b)-abs(ppad-idx):]
                        ttext = ["?"] * ppad + b + \
                            a[len(b)-abs(ppad-idx):] + ["?"] * ppad
                    else:
                        plane = a + b[len(a)-abs(ppad-idx):]
                        ttext = ["?"] * pad + a + \
                            b[len(a)-abs(ppad-idx):] + ["?"] * ppad
                    ttable = partial_match_table(c)
                    # print(ttext)

                    ii, pp = idx, 0

                    while ii < len(ttext) and pp < len(c):
                        if ttext[ii] == c[pp] or ttext[ii] == "?" or c[pp] == "?":
                            ii += 1
                            pp += 1
                            if pp == len(c):
                                # res.append(abs(ppad - (ii - pp)))
                                iidx = ii - pp
                                if iidx < ppad:
                                    res.append(ppad-iidx + len(plane))
                                else:
                                    res.append(len(plane) + iidx - ppad)
                                pp = ttable[pp]

                        elif pp == 0:
                            ii += 1
                        else:
                            pp = ttable[pp]

                    p = table[p]

            elif p == 0:
                i += 1
            else:
                p = table[p]

    kmp_search(a, b, c)
    kmp_search(a, c, b)
    kmp_search(b, a, c)
    kmp_search(b, c, a)
    kmp_search(c, a, b)
    kmp_search(c, b, a)
    print(min(res))


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    a = next(tokens)  # type: str
    b = next(tokens)  # type: str
    c = next(tokens)  # type: str
    solve(a, b, c)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def conbine(a,b):
    result=a+b
    for i in range(len(a)):
        flag=True
        tmp=a[:i]
        for j in range(len(b)):
            if len(a)<=i+j:
                tmp+=b[j:]
                break
            if not (a[i+j]==b[j] or a[i+j]=='?' or b[j]=='?'):
                flag=False
                break
            
            if a[i+j]=='?':
                tmp+=b[j]
            else:
                tmp+=a[i+j]

        tmp+=a[len(tmp)+1:]

        if flag:
            result=tmp if len(tmp)<len(result) else result
    
    return result

def main():
    import sys
    import math
    dic={}
    dic['a']=sys.stdin.readline().strip()
    dic['b']=sys.stdin.readline().strip()
    dic['c']=sys.stdin.readline().strip()

    result=7000

    abc=['a','b','c']
    for alp1 in abc:
        for alp2 in abc:
            if alp1==alp2:
                continue
            for alp3 in abc:
                if alp3==alp2 or alp3==alp1:
                    continue

                ts=conbine(dic[alp1],dic[alp2])
                s=conbine(ts,dic[alp3])
                result=len(s) if len(s) < result else result


    return result

print(main())

Submission status: Wrong Answer

Solution code:

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def jojo(a,b):
    minlen = min(len(a),len(b))
    n=0
    for i in range(minlen):
        if a[-1*i]=="?" or b[i]=="?":
            n+=1
        elif a[-1*(i+1)]==b[i]:
            n+=1
        else:
            return n
    return n

def resolve():
    readline=sys.stdin.readline

    a=readline().strip()
    b=readline().strip()
    c=readline().strip()

    abc=len(a)+len(b)+len(c)
    #p1=jojo(a,b)+jojo(b,c)
    #p2=jojo(a,c)+jojo(c,b)
    #p3=jojo(b,a)+jojo(a,c)
    #p4=jojo(b,c)+jojo(c,a)
    #p5=jojo(c,a)+jojo(a,b)
    #p6=jojo(c,b)+jojo(b,a)
    bbb=max(jojo(a,b),jojo(b,a),jojo(b,c),jojo(c,b))
    ccc=max(jojo(a,c),jojo(c,a),jojo(b,c),jojo(c,b))
    aaa=max(jojo(a,b),jojo(b,a),jojo(a,c),jojo(c,a))

    #print(abc-max(p1,p2,p3,p4,p5,p6))
    print(abc-bbb-ccc-aaa+min(aaa,bbb,ccc))
    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

Submission status: Wrong Answer

Solution code:
a = input()
b = input()
c = input()
La = len(a)
Lb = len(b)
Lc = len(c)

def func(a, b):
    ret = set()
    for i in range(-len(b)+1, len(a)):
        ok = True
        for j in range(max(0, i), min(len(a), len(b)+i)):
            if a[j] != '?' and b[j-i] != '?' and a[j] != b[j-i]:
                ok = False
                break
        if ok:
            ret.add(i)
    return ret

L1 = func(a, b)
L2 = func(a, c)
L3 = func(b, c)
L1.add(-Lb)
L1.add(La)
L2.add(-Lc)
L2.add(La)
L3.add(-Lc)
L3.add(Lb)
ans = 10000
for i in L1:
    for j in L2:
        if j - i in L3:
            lo = min(0, i, j)
            hi = max(len(a), i+len(b), j+len(c))
            ans = min(ans, hi-lo)
    for k in L3:
        j = i + k
        if j >= La or j <= -Lc:
            lo = min(0, i, j)
            hi = max(len(a), i+len(b), j+len(c))
            ans = min(ans, hi-lo)
for j in L2:
    for k in L3:
        i = j - k
        if i >= La or i <= -Lb:
            lo = min(0, i, j)
            hi = max(len(a), i+len(b), j+len(c))
            ans = min(ans, hi-lo)
print(ans)

Submission status: Wrong Answer

Solution code:
def inner(a,b,la,lb):
  for i in range(lb-la+1):
    tmp=''
    for j in range(la):
      if a[j]=='?' or a[j]==b[i+j]:
        tmp+=b[i+j]
        continue
      elif b[i+j]=='?':
        tmp+=a[j]
        continue
      else:
        break
    else:
      return True
  return False

def head(a,b,la):
  for i in range(la-1,-1,-1):
    tmp=''
    for j in range(i):
      if a[j]=='?' or a[j]==b[-(i-j)]:
        tmp+=b[-(i-j)]
        continue
      elif b[-(i-j)]=='?':
        tmp+=a[j]
        continue
      else:
        break
    else:
      return b+a[i:]

def tail(a,b,la):
  for i in range(la-1,-1,-1):
    tmp=''
    for j in range(i):
      if a[-(i-j)]=='?' or a[-(i-j)]==b[j]:
        tmp+=b[j]
        continue
      elif b[j]=='?':
        tmp+=a[-(i-j)]
        continue
      else:
        break
    else:
      return a+b[i:]
      break

a=input()
la=len(a)
b=input()
lb=len(b)
c=input()
lc=len(c)
arr=[[a,la],[b,lb],[c,lc]]
arr=sorted(arr,key=lambda x:x[1])
a,la=arr[0]
b,lb=arr[1]
c,lc=arr[2]
tmp1=head(a,c,la)
tmp2=tail(a,c,la)
ans1=min(len(head(b,tmp1,lb)),len(tail(b,tmp1,lb)))
if inner(b,tmp1,lb,len(tmp1)):
  ans1=min(ans1,len(tmp1))
ans2=min(len(head(b,tmp2,lb)),len(tail(b,tmp2,lb)))
if inner(b,tmp2,lb,len(tmp2)):
  ans2=min(ans2,len(tmp2))
ans3=10**18
if inner(a,c,la,lb):
  ans3=min(len(head(b,c,lb)),len(tail(b,c,lb)))
  if inner(b,c,lb,lc):
    ans3=min(ans3,lc)
print(min(ans1,ans2,ans3))

Submission status: Wrong Answer

Solution code:
A = list(input())
B = list(input())
C = list(input())
for i in [A[-1],B[-1],C[-1]]:
    if not (ord('a') <= ord(i) <= ord('z') or i =='?'):
        print(1/0)

def marge(X,Y,n):
    ans = X
    for i in range(n,n+len(Y)):
        if i >= len(X):
            return ans + Y[i-n:]
        if ans[i] == "?":
            ans[i] = Y[i-n]
    return ans

def func(X,Y):
    for i in range(len(X)):
        flag = True
        for j in range(len(Y)):
            if i+j >= len(X):
                return marge(X,Y,i)
            if X[i+j] != Y[j] and X[i+j] != "?" and Y[j] != "?":
                flag = False
                break
        if flag:
            return marge(X,Y,i)
    return X+Y

print(func(A,B))

print(min(len(func(func(A,B),C)),
        len(func(func(A,C),B)),
        len(func(func(B,A),C)),
        len(func(func(B,C),A)),
        len(func(func(C,B),A)),
        len(func(func(C,A),B)),))

Submission status: Wrong Answer

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

def union(a,b):
  db=0
  for i in range(len(a)):
    to=0
    for j in range(len(b)):
      if i+j==len(a):
        to=len(b)-j
        break
      elif a[i+j]==b[j] or a[i+j]=="?" or b[j]=="?":
        pass
      else:
        to=-1
        break
    if to==0:
      db=a
      for bi in range(len(b)):
        if db[i+bi]=="?":
          db[i+bi]=b[bi]
      break
    elif to>0:
      db=a
      for bi in range(len(b)-to):
        if db[-(len(b)-to)+bi]=="?":
          db[-(len(b)-to)+bi]=b[bi]
      db=db+b[-to:]
      break
  if db==0:
    
    db=a+b
  return db

ans=float("inf")
for x,y,z in [[a,b,c],[b,a,c],[b,c,a],[c,b,a],[c,a,b],[a,c,b]]:
  ans=min(ans,len(union(union(x,y),z)),len(union(z,union(x,y))))

    
print(ans)

Submission status: Wrong Answer

Solution code:
a = list(input())
b = list(input())
c = list(input())
def f(a,b):
    nab = 0
    for i in range(len(a)):
        flag = True
        for j in range(min(len(b),len(a)-i)):
            if a[i+j] != '?' and b[j] != '?' and a[i+j] != b[j]:
                flag = False
        if flag:
            if len(b) <= len(a)-i:
                nab = -i-1
            else:
                nab = len(a)-i
            break
    if nab < 0:
        ab = ['?'] * len(a)
        for i in range(len(a)):
            if 0 <= i+1+nab < len(b):
                ab[i] = a[i] if a[i] != '?' else b[i+1+nab]
            else:
                ab[i] = a[i]
    else:
        ab = ['?'] * (len(a)+len(b)-nab)
        for i in range(len(ab)):
            if i < len(a)-nab:
                ab[i] = a[i]
            elif len(a)-nab<=i<len(a):
                ab[i] = a[i] if a[i] != '?' else b[i+nab-len(a)]
            else:
                ab[i] = b[i+nab-len(a)]
    return ab
print(min(len(f(f(a,b),c)),len(f(f(b,a),c)),len(f(f(a,c),b)),len(f(f(b,c),a)),len(f(f(c,a),b)),len(f(f(c,b),a)),len(f(a,f(b,c))),len(f(b,f(a,c))),len(f(c,f(a,b))),len(f(a,f(c,b))),len(f(b,f(c,a))),len(f(c,f(b,a)))))

Submission status: Wrong Answer

Solution code:
a=input()
b=input()
c=input()
X=[a,b,c]

def mix(s,t):
    if len(s)<len(t):
        s="?"*(len(t)-len(s))+s
    for length in range(min(len(t),len(s)),0,-1):
        u=s[-length:]
        v=t[:length]

        #print(u,v)

        for i in range(length):
            if u[i]==v[i] or u[i]=="?" or v[i]=="?":
                continue
            else:
                break
        else:
            ret=""
            for i in range(length):
                if u[i]=="?":
                    ret+=v[i]
                else:
                    ret+=u[i]
                
            yield s[:-length]+ret+t[length:]

    yield s+t

from itertools import permutations
ITE=permutations([0,1,2])
ANS=[]
for i,j,k in ITE:
    for l in mix(X[i],X[j]):
        ANS.extend(list(mix(l,X[k])))
        ANS.extend(list(mix(X[k],l)))

L=1<<30
for a in ANS:
    L=min(L,len(a))
print(L)

Submission status: Wrong Answer

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


def stack2(a, b):
    max_reduce = min(len(a), len(b))
    reduce = 0
    for i in range(max_reduce):
        a_end = a[len(a)-i-1:]
        b_begin = b[:i+1]
        flag = True
        for j in range(len(a_end)):
            if a_end[j] != b_begin[j] and a_end[j] != '?' and b_begin[j] != '?':
                flag = False
        if flag == True:
            reduce = len(a_end)
    return reduce

S = len(a) + len(b) + len(c)

r1 = stack2(a, b) + stack2(b, c)
r2 = stack2(a, c) + stack2(c, b)
r3 = stack2(b, a) + stack2(a, c)
r4 = stack2(b, c) + stack2(c, a)
r5 = stack2(c, a) + stack2(a, b)
r6 = stack2(c, b) + stack2(b, a)

r = max(r1, r2, r3, r4, r5, r6)

print(S-r)

Submission status: Wrong Answer

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

ls = [a, b, c]

mix_list = []
# 順番を決定する
ans = 2000 * 3
for i in range(len(ls)):
    left = ls[i]
    for j in range(len(ls)):
        if i != j:
            mid = ls[j]
            for k in range(len(ls)):
                if not k in [i,j]:
                    right = ls[k]
                    # 一番合致するところで合体させる
                    for n in range(min(len(left), len(mid)) - 1, -1, -1):
                        lm = left + mid
                        if left[-n] == mid[0] or left[-n]=='?' or mid[0]=='?':
                            is_connect = True
                            for m in range(n):
                                if left[-n] != mid[m] or left[-n]=='?' or mid[m]=='?':
                                    is_connect = False
                                    break
                            if is_connect:
                                lm = left[:-n] + mid
                        lmr = lm + right
                        for n in range(min(len(lm), len(right)) - 1, -1, -1):
                            if lm[-n] == right[0] or lm[-n] == '?' or right[0] == '?':
                                is_connect = True
                                for m in range(n):
                                    if lm[-n] != right[m] or lm[-n] == '?' or right[m] == '?':
                                        is_connect = False
                                        break
                                if is_connect:
                                    lmr = lm[:-n] + right
                        ans = min(len(lmr), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
def check(a,b,c):
    ma = 0
    ml = min(len(a),len(b))
    for i in range(ml):
        f = False
        for j in range(ml-i):
            # print('pair', a[i+j], b[ml-1-i-j])
            if (a[-j-1] == b[ml-1-i-j]) or a[-1-j] == '?' or b[ml-1-i-j] == '?':
                # print(a[i+j], b[ml-1-i-j])
                continue
            else:
                f = True
                break
        if not f:
            ma = ml -i
            break
    
    for i in range(ma):
        if b[i] == '?' and a[-1-i] != '?':
            b = list(b)
            b[i] = a[-1-i]
            b = ''.join(b)
        
    mb = 0
    ml = min(len(b),len(c))
    for i in range(ml):
        f = False
        for j in range(ml-i):
            if (b[-j-1] == c[ml-1-i-j]) or b[-1-j] == '?' or c[ml-1-i-j] == '?':
                continue
            else:
                f = True
                break
        if not f:
            mb = ml -i
            break
    # print(len(a)-ma , len(c) - mb , len(b))
    return len(a)-ma + len(c) - mb + len(b)

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

s = float('inf')
s = min(s, check(a,b,c))
s = min(s, check(a,c,b))
s = min(s, check(b,c,a))
s = min(s, check(b,a,c))
s = min(s, check(c,b,a))
s = min(s, check(c,a,b))
print(s)

Submission status: Wrong Answer

Solution code:
A = input()
B = input()
C = input()


def check(S, T):
    """
    S,Tの順に結合する
    """
    Ns = len(S)

    for i, t in enumerate(T):
        if t != "?":
            break
    T = T[i:]
    Nt = len(T)
    match = 0

    for i, s in enumerate(S):
        if s == T[0] or s == "?":
            x = i
            y = 0
            while x < Ns and y < Nt:
                if S[x] == "?" or T[y] == "?" or S[x] == T[y]:
                    x += 1
                    y += 1
                    continue
                else:
                    break
            else:
                match = max(match, y)
    suffix = T[match:]
    return S + suffix


abc = check(check(A, B), C)
acb = check(check(A, C), B)
bac = check(check(B, A), C)
bca = check(check(B, C), A)
cab = check(check(C, A), B)
cba = check(check(C, B), A)
# print(abc)
# print(acb)
# print(bac)
# print(bca)
# print(cab)
# print(cba)

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