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:
import sys,collections as cl,bisect as bs,math
from decimal import *
getcontext().prec = 50
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = mod
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

def search(x,y):
    lx = len(x)
    ly = len(y)
    for i in range(lx):
        for j in range(min(ly,lx-(i))):
            if x[i+j] == "?" or y[j] == "?" or x[i+j] == y[j]:
                continue
            else:
                break
        else:
            return i
    else:
        return lx

a = input().split()[0]
b = input().split()[0]
c = input().split()[0]

aa = len(a)

bb = len(b)

cc = len(c)

ab = search(a,b)

ba = search(b,a)

bc = search(b,c)

cb = search(c,b)

ca = search(c,a)

ac = search(a,c)

po = Max

n = 6000
onon = [0 for i in range(6000)]

for i in range(6000):
    if i <= len(a)-1:
        onon[i] += 1
    if i <= ab + len(b) -1:
        onon[i] += 1
    if i <= ab + bc + len(c) -1:
        onon[i] += 1

co = 0
for i in range(n):
    if onon[i] > 0:
        co += 1
po = min(po,co)




onon = [0 for i in range(6000)]

for i in range(6000):
    if i <= len(a)-1:
        onon[i] += 1
    if i <= ac + len(c) -1:
        onon[i] += 1
    if i <= ac + cb + len(b) -1:
        onon[i] += 1

co = 0
for i in range(n):
    if onon[i] > 0:
        co += 1
po = min(po,co)



onon = [0 for i in range(6000)]

for i in range(6000):
    if i <= len(c)-1:
        onon[i] += 1
    if i <= cb + len(b) -1:
        onon[i] += 1
    if i <= cb + ba + len(a) -1:
        onon[i] += 1

co = 0
for i in range(n):
    if onon[i] > 0:
        co += 1
po = min(po,co)


onon = [0 for i in range(6000)]

for i in range(6000):
    if i <= len(b)-1:
        onon[i] += 1
    if i <= ba + len(a) -1:
        onon[i] += 1
    if i <= ba + ac + len(c) -1:
        onon[i] += 1

co = 0
for i in range(n):
    if onon[i] > 0:
        co += 1
po = min(po,co)



onon = [0 for i in range(6000)]

for i in range(6000):
    if i <= len(b)-1:
        onon[i] += 1
    if i <= bc + len(c) -1:
        onon[i] += 1
    if i <= bc + ca + len(a) -1:
        onon[i] += 1

co = 0
for i in range(n):
    if onon[i] > 0:
        co += 1
po = min(po,co)


onon = [0 for i in range(6000)]

for i in range(6000):
    if i <= len(c)-1:
        onon[i] += 1
    if i <= ca + len(a) -1:
        onon[i] += 1
    if i <= ca + ab + len(b) -1:
        onon[i] += 1

co = 0
for i in range(n):
    if onon[i] > 0:
        co += 1
po = min(po,co)

print(po)

Submission status: Wrong Answer

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
A=input()
B=input()
C=input()
def chk(X,Y,i):
  k=min(len(X)-i,len(Y))
  for j in range(k):
    if X[i+j]=="?" or Y[j]=="?" or X[i+j]==Y[j]:
      continue
    else:
      return False
  else:
    return True
a,b,c=len(A),len(B),len(C)
ans=a+b+c
def f(X,Y,Z):
  x,y,z=len(X),len(Y),len(Z)
  xy=0
  for i in range(x):
    if chk(X,Y,i):
      xy=i+1
      break

  if xy==0:
    XY=X+Y
  else:
    XY=X[:xy-1]
    for i in range(y):
      if xy-1+i<x and X[xy-1+i]=="?" and Y[i]=="?":
        XY+="?"
      elif xy-1+i<x and X[xy-1+i]!="?":
        XY+=X[xy-1+i]
      else:
        XY+=Y[i]
  if x>xy-1+y:
    XY+=X[xy-1+y:]
  if xy==0:
    xy=x+y
  else:
    xy=max(xy-1+y,x)
  xyz=0
  for i in range(xy):
    if chk(XY,Z,i):
      xyz=i+1
  if xyz==0:
    return xy+z
  else:
    return max(xy,xyz-1+z)

ans=min(ans,f(A,B,C))
ans=min(ans,f(A,C,B))
ans=min(ans,f(B,A,C))
ans=min(ans,f(B,C,A))
ans=min(ans,f(C,A,B))
ans=min(ans,f(C,B,A))
print(ans)

Submission status: Wrong Answer

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

def eq_wc(s1,s2):
  len_s=len(s1)
  for i in range(len_s):
    if s1[i]!=s2[i] and s1[i]!="?" and s2[i]!="?":
      return False
  return True

def merge_abc(aa,bb,cc):
  #print(aa,bb,cc)
  match=0
  for i in range(1,min(len(aa),len(bb))+1):
    sub1=aa[-i:]
    sub2=bb[:i]
    #print(sub1,sub2)
    if eq_wc(sub1,sub2):
      match+=1
    else:
      break

  ab=[]
  for i in range(len(aa)-match):
    ab.append(aa[i])
  for i in range(match):
    if aa[len(aa)-match+i]==b[i]:
      ab.append(bb[i])
    elif aa[len(aa)-match+i]=="?":
      ab.append(bb[i])
    else:
      ab.append(aa[len(aa)-match+i])
  for i in range(len(bb)-match):
    ab.append(bb[i+match])  

  match=0
  for i in range(1,min(len(ab),len(cc))+1):
    sub1=ab[-i:]
    sub2=cc[:i]
    #print(sub1,sub2)
    if eq_wc(sub1,sub2):
      match+=1
    else:
      break
  abc=[]
  for i in range(len(ab)-match):
    abc.append(ab[i])
  for i in range(match):
    if ab[len(ab)-match+i]==cc[i]:
      abc.append(cc[i])
    elif ab[len(ab)-match+i]=="?":
      abc.append(cc[i])
    else:
      abc.append(ab[len(ab)-match+i])
  for i in range(len(cc)-match):
    abc.append(cc[i+match])
  
  #print(len(abc),abc)
  return abc

abc=merge_abc(a,b,c)
acb=merge_abc(a,c,b)
bac=merge_abc(b,a,c)
bca=merge_abc(b,c,a)
cab=merge_abc(c,a,b)
cba=merge_abc(c,b,a)
  
print(min(len(abc),len(acb),len(bac),len(bca),len(cab),len(cba)))

Submission status: Wrong Answer

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


def solve(a: str, b: str, c: str):
    def is_equal(s1,s2):
        if len(s1) != len(s2):
            return False
        
        for i in range(len(s1)):
            if s1[i] == s2[i] or s1[i]== "?" or s2[i] == "?":
                continue
            else:
                return False
        return True

    junban = [(0,1,2),(0,2,1),(1,0,2),(1,2,0),(2,1,0),(2,0,1)]
    abc = [a,b,c]

    ans = 10**9
    for i_1,j_1,k_1 in junban:
        aa = abc[i_1]
        bb = abc[j_1]
        cc = abc[k_1]
        kouho = []
        LENA = len(a)
        for i in range(len(aa)):
            if is_equal(aa[i:],bb[:len(a)-i]):
                kouho.append(aa[:i]+bb)
        kouho.append(aa+bb)

        answer = []
        for d in kouho:
            for i in range(len(d)):
                if is_equal(d[i:],cc[:len(d)-i]):
                    ans = min(ans,len(d[:i]+cc))
            ans = min(ans,len(d+cc))

    print(ans)
    return


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 match(c, d):
    return c == '?' or d == '?' or c == d
def cross_head(s, t):
    for i in range(len(s) + 1):
        match_all = True
        for j in range(i, min(len(s), len(t) + i)):
            if not match(s[j], t[j - i]):
                match_all = False
        if match_all:
            return i
a = input()
b = input()
c = input()
candis = []
def app(x, y, z):
    nxy = cross_head(x, y)
    xy = x[:nxy] + y
    candis.append(min(cross_head(xy, z) + len(z), cross_head(z, xy) + len(xy)))
app(a,b,c)
app(b,c,a)
app(c,a,b)
app(a,c,b)
app(c,b,a)
app(b,a,c)
print(min(candis))

Submission status: Wrong Answer

Solution code:
import sys
from itertools import permutations

a, b, c = sys.stdin.read().split()

def merge(s, t):
    n = len(s)
    m = len(t)
    cand = []
    for i in range(n):
        for j in range(min(m, n-i)):
            if s[i+j] != t[j]:
                if s[i+j] != '?' and t[j] != '?':
                    break
        else:
            if m >= n - i:
                cand.append(s[:i] + t)
            else:
                cand.append(s)
                break
    return cand

def main():
    ans = 10000
    for p in permutations((a, b, c)):
        s, t, u = p
        cand = merge(s, t)
        for w in cand:
            res = merge(w, u)
            for r in res:
                ans = min(ans, len(r))
    return ans

if __name__ == "__main__":
    ans = main()
    print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import permutations
import collections
abc = [input() for i in range(3)]


ans = 10000

for x in permutations(range(3)):
    a = abc[x[0]]
    b = abc[x[1]]
    c = abc[x[2]]
    id = 0
    ab = len(a)
    bc = len(b)
    for i in range(len(a)):
        if a[i] == b[0] or a[i] == "?" or b[0] == "?":
            count = 1
            for j in range(1, min(len(a) - i, len(b))):
                if a[i+j] == b[j] or a[i+j] == "?" or b[j] == "?":
                    count += 1
                else:
                    break
            if count + i == len(a):
                ab = i
                break
            elif count == len(b):
                break
            else:
                continue
    if b == "?"*len(b):
        b = a
    for i in range(len(b)):
        if b[i] == c[0] or b[i] == "?" or c[0] == "?":
            count = 1
            for j in range(1, min(len(b) - i, len(c))):
                if b[i+j] == c[j] or b[i+j] == "?" or c[j] == "?":
                    count += 1
                else:
                    break
            if count + i == len(b):
                bc = i
                break
            elif count == len(b):
                break
            else:
                continue

    tmp = ab + bc + len(c)

    ans = min(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
a=input()
a_l=len(a)
b=input()
b_l=len(b)
c=input()
c_l=len(c)
def check(s1,s2):
    l1=len(s1)
    l2=len(s2)
    ret=[0]*(l1+l2-1)
    for i in range(l1+l2-1):
        flag=True
        if i-l2+1<0 and i<=l1-1:
            for j in range(i+1):
                if s1[j]!="?" and s2[l2-i+j-1]!="?" and s1[j]!=s2[l2-i+j-1]:
                    flag=False
                    break
        elif i-l2+1<0 and i>l1-1:
            for j in range(l1):
                if s1[j]!="?" and s2[l2-i+j-1]!="?" and s1[j]!=s2[l2-i+j-1]:
                    flag=False
                    break
        elif i-l2+1>=0 and i<=l1-1:
            for j in range(l2):
                if s1[i-l2+j+1]!="?" and s2[j]!="?" and s1[i-l2+j+1]!=s2[j]:
                    flag=False
                    break
        else:
            for j in range(l1+l2-i-1):
                if s1[i-l2+j+1]!="?" and s2[j]!="?" and s1[i-l2+j+1]!=s2[j]:
                    flag=False
                    break
        ret[i]=flag
    return ret
ab=check(a,b)
ab_l=len(ab)
ac=check(a,c)
ac_l=len(ac)
bc=check(b,c)
bc_l=len(bc)
ans=a_l+b_l+c_l
for i in range(-2000,ab_l+2000):
    if i<0 or i>=ab_l or ab[i]:
        for j in range(-2000,ac_l+2000):
            if j<0 or j>=ac_l or ac[j]:
                if j-i+b_l+1<0 or j-i+b_l+1>=bc_l or bc[j-i+b_l+1]:
                    l=max(a_l-1,i,j)-min(0,i-b_l+1,j-c_l+1)+1
                    ans=min(ans,l)
print(ans)

Submission status: Wrong Answer

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

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

def merge(X, Y):
  ret = []
  for i in range(len(X)):
    s = X[:i]
    for x, y in zip(X[i:], Y):
      if x != '?' and y != '?' and x != y:
        break
      else:
        s += x if x != '?' else y
    else:
      ret.append(s + Y[len(X)-i:])
  return ret

for D in merge(A, B) + merge(B, A):
  if len(D) >= ans:
    break
  ans = min(ans, len(D) + len(C))
  for E in merge(D, C) + merge(C, D):
    ans = min(ans, len(E))

for D in merge(A, C) + merge(C, A):
  if len(D) >= ans:
    break
  ans = min(ans, len(D) + len(B))
  for E in merge(D, B) + merge(B, D):
    ans = min(ans, len(E))

for D in merge(B, C) + merge(C, B):
  if len(D) >= ans:
    break
  ans = min(ans, len(D) + len(A))
  for E in merge(D, A) + merge(A, D):
    ans = min(ans, len(E))

print(ans)

Submission status: Wrong Answer

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


def solve(a: str, b: str, c: str):
    def is_equal(s1,s2):
        if len(s1) != len(s2):
            return False
        
        for i in range(len(s1)):
            if s1[i] == s2[i] or s1[i]== "?" or s2[i] == "?":
                continue
            else:
                return False
        return True

    junban = [(0,1,2),(0,2,1),(1,0,2),(1,2,0),(2,1,0),(2,0,1)]
    abc = [a,b,c]

    ans = 10**9
    for i_1,j_1,k_1 in junban:
        aa = abc[i_1]
        bb = abc[j_1]
        cc = abc[k_1]
        kouho = []
        LENA = len(a)
        for i in range(len(aa)):
            if is_equal(aa[i:],bb[:len(a)-i]):
                kouho.append(aa[:i]+bb)
        kouho.append(aa+bb)

        answer = []
        for d in kouho:
            for i in range(len(d)):
                if is_equal(d[i:],cc[:len(d)-i]):
                    answer.append(d[:i]+cc)
            answer.append(d+cc)

        mac = 10**9
        for i in range(len(answer)):
            mac = min(mac,len(answer[i]))   
        ans = min(ans,mac)

    print(ans)
    return


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:
from itertools import permutations
import collections
abc = [input() for i in range(3)]


ans = 10000

for x in permutations(range(3)):
    a = abc[x[0]]
    b = abc[x[1]]
    c = abc[x[2]]

    id = 0
    ab = len(a)
    bc = len(b)
    for i in range(len(a)):
        if a[i] == b[0] or a[i] == "?" or b[0] == "?":
            count = 1
            for j in range(1, min(len(a) - i, len(b))):
                if a[i+j] == b[j] or a[i+j] == "?" or b[j] == "?":
                    count += 1
                else:
                    break
            if count + i == len(a):
                ab = i
                break
            elif count == len(b):
                break
            else:
                continue

    for i in range(len(b)):
        if b[i] == c[0] or b[i] == "?" or c[0] == "?":
            count = 1
            for j in range(1, min(len(b) - i, len(c))):
                if b[i+j] == c[j] or b[i+j] == "?" or c[j] == "?":
                    count += 1
                else:
                    break
            if count + i == len(b):
                bc = i
                break
            elif count == len(b):
                break
            else:
                continue

    tmp = ab + bc + len(c)
    b = a
    bc = len(b)
    for i in range(len(b)):
        if b[i] == c[0] or b[i] == "?" or c[0] == "?":
            count = 1
            for j in range(1, min(len(b) - i, len(c))):
                if b[i+j] == c[j] or b[i+j] == "?" or c[j] == "?":
                    count += 1
                else:
                    break
            if count + i == len(b):
                bc = i
                break
            elif count == len(b):
                break
            else:
                continue
    tmp = max(tmp, ab + bc + len(c))
    ans = min(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    a = [c for c in input()]
    b = [c for c in input()]
    c = [c for c in input()]

    ans = 7000
    for x, y, z in [(a, b, c), (a, c, b), (b, a, c), (b, c, a), (c, a, b), (c, b, a)]:
        m = cct(x, y)
        x = binsearch(0, 7000, lambda i: ok(i, m, z))
        ans = min(x, ans)
    print(ans)

def ok(n, xs, b):
    for a in xs:
        if len(a) + len(b) <= n:
            return True
        if len(a) > n:
            continue
        if len(b) > n:
            continue
        o = True
        for i in range(n-len(b), len(a)):
            k = i + len(b) - n
            if a[i] != b[k] and a[i] != '?' and b[k] != '?':
                o = False
                break
        if o:
            return True
    
def binsearch(i, j, ok):
    if i+1 == j:
        return j
    m = (i + j) // 2
    if ok(m):
        return binsearch(i, m, ok)
    else:
        return binsearch(m, j, ok)


def cct(a, b):
    c = [a+b]
    for i in range(len(a)):
        z = list(a)
        f = True
        for j in range(len(b)):
            if i+j >= len(a):
                break
            elif a[i+j] != b[j]:
                if a[i+j] == '?':
                    z[i+j] = b[j]
                elif b[j] != '?':
                    f = False
                    break
        if f:
            if i+len(b) > len(a):
                z.extend(b[len(a)-i:])
            c.append(z)
    return c



main()

Submission status: Wrong Answer

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

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

def tail(a,b,la):
  for i in range(la,-1,-1):
    for j in range(i):
      if a[-(i-j)]=='?' or a[-(i-j)]==b[j]:
        continue
      elif b[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,lc):
  ans3=min(len(head(b,c,lb)),len(tail(b,c,lb)))
  if inner(b,c,lb,lc):
    ans3=min(ans3,lc)
ansac=min(ans1,ans2,ans3)
tmp1=head(b,c,lb)
tmp2=tail(b,c,lb)
ans1=min(len(head(a,tmp1,la)),len(tail(a,tmp1,la)))
if inner(a,tmp1,la,len(tmp1)):
  ans1=min(ans1,len(tmp1))
ans2=min(len(head(a,tmp2,la)),len(tail(a,tmp2,la)))
if inner(a,tmp2,la,len(tmp2)):
  ans2=min(ans2,len(tmp2))
ans3=10**18
if inner(b,c,lb,lc):
  ans3=min(len(head(a,c,la)),len(tail(a,c,la)))
  if inner(a,c,la,lc):
    ans3=min(ans3,lc)
ansbc=min(ans1,ans2,ans3)
ans=min(ansac,ansbc)
print(ans)

Submission status: Wrong Answer

Solution code:
l = ["","",""]
d = [["","",""],["","",""],["","",""]]
l[0] = str(input())
l[1] = str(input())
l[2] = str(input())
for i in range(3):
  for j in range(3):
    if i!=j:
      p = min(len(l[i]),len(l[j]))
      s = 0
      for k in reversed(range(p)):
        t = 0
        for x in range(k+1):
          if l[i][x-k-1]!=l[j][x]:
            if ((l[i][x-k-1]!="?") & (l[j][x]!="?")):
              t = 1
              break
        if t==0:
          s = k+1
          break
      d[i][j] = s
r = len(l[0])+len(l[1])+len(l[2])
u = (d[0][1]+d[1][2]+d[2][0])-min(min(d[0][1],d[1][2]),d[2][0])
v = (d[1][0]+d[2][1]+d[0][2])-min(min(d[1][0],d[2][1]),d[0][2])
print(r-max(u,v))

Submission status: Wrong Answer

Solution code:
def main():
    a = [c for c in input()]
    b = [c for c in input()]
    c = [c for c in input()]

    ans = 7000
    for x, y, z in [(a, b, c), (a, c, b), (b, a, c), (b, c, a), (c, a, b), (c, b, a)]:
        m = cct(x, y)
        x = binsearch(0, 7000, lambda i: ok(i, m, z))
        ans = min(x, ans)
    print(ans)

def ok(n, xs, b):
    for a in xs:
        if len(a) + len(b) <= n:
            return True
        if len(a) > n:
            return False
        if len(b) > n:
            return False
        o = True
        for i in range(n-len(b), len(a)):
            k = i + len(b) - n
            if a[i] != b[k] and a[i] != '?' and b[k] != '?':
                o = False
                break
        if o:
            return True
    
def binsearch(i, j, ok):
    if i+1 == j:
        return j
    m = (i + j) // 2
    if ok(m):
        return binsearch(i, m, ok)
    else:
        return binsearch(m, j, ok)


def cct(a, b):
    c = [a+b]
    for i in range(len(a)):
        z = list(a)
        f = True
        for j in range(len(b)):
            if i+j >= len(a):
                break
            elif a[i+j] != b[j]:
                if a[i+j] == '?':
                    z[i+j] = b[j]
                elif b[j] != '?':
                    f = False
                    break
        if f:
            if i+len(b) > len(a):
                z.extend(b[len(a)-i:])
            c.append(z)
    return c



main()

Submission status: Wrong Answer

Solution code:
import sys
from itertools import permutations

def solve():
    words = [input() for _ in range(3)]
    heads = [words[i][0] for i in range(3)]
    length = [len(words[i]) for i in range(3)]
    Ans = sum(length)
    for top, middle, end in permutations(range(3)):
        for i in range(length[top]):
            if words[top][i] == heads[middle] or words[top][i] == "?" or heads[middle] == "?":
                for fc in range(min(length[top] - i, length[middle]) - 1):
                    if words[top][i + fc + 1] == words[middle][fc + 1] or words[top][i + fc + 1] == "?" or words[middle][fc + 1] == "?": continue
                    else: break
                else:
                    for j in range(length[middle]):
                        if words[middle][j] == heads[end] or words[middle][j] == "?" or heads[end] == "?":
                            if i + j < length[top]:
                                if words[top][i + j] == heads[end] or words[top][i + j] == "?" or heads[end] == "?": continue
                                else: break
                            for fd in range(min(length[middle] - j, length[end]) - 1):
                                if words[middle][j + fd + 1] == words[end][fd + 1] or words[middle][j + fd + 1] == "?" or words[end][fd + 1] == "?":
                                    if i + j + fd + 1 < length[top]:
                                        if words[top][i + j + fd + 1] == words[end][fd + 1] or words[top][i + j + fd + 1] == "?" or words[end][fd + 1] == "?": continue
                                        else: break
                                else: break
                            else: 
                                m = max(length[middle], j + length[end])
                                l = max(length[top], i + m)
                                Ans = min(Ans, l)
    print(Ans)
                                
    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

def ch(x, y, k):
    nx, ny = len(x)-k, len(y)
    for i in range(nx):
        # x[i:], y[0:]
#        print(nx-i)
        for j in range(min(nx-i, ny)):
#            print(x[i+j], y[j], y, j)
            if x[k+i+j] != '?' and y[j] != '?' and x[k+i+j] != y[j]:
                break
        else:
            return i
    return nx
R = len(a)+len(b)+len(c)
for d, e, f in [(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)]:
    i = ch(X[d], X[e], 0)
    if len(X[d])-i >= len(X[e]):
        j = ch(X[d], X[f], i)
    else:
#        print(X[d])
        j = ch(X[e], X[f], 0)
#    print(X[d], X[e], X[f], i, j, len(X[f]), i+j+len(X[f]))
    R = min(R, i+j+max(len(X[f]), len(X[e])-j))
print(R)

Submission status: Wrong Answer

Solution code:
import re

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

_a = [[0,0], [0,0]]
_b = [[0,0], [0,0]]
_c = [[0,0], [0,0]]

for i in range(min(len(a), len(b)), 0, -1):
  a_re = a[(len(a)-i):].replace('?',"[a-z]")
  pattern = re.compile(r"%s" % a_re)
  res = pattern.search(b[:i+1])
  if res is not None:
    _a[0][0] = i
    _b[0][1] = i
    break

for i in range(min(len(a), len(c)), 0, -1):
  a_re = a[(len(a)-i):].replace('?',"[a-z]")
  pattern = re.compile(r"%s" % a_re)
  res = pattern.search(c[:i+1])
  if res is not None and res.group() != "":
    _a[1][0] = i
    _c[0][1] = i
    break

for i in range(min(len(b), len(c)), 0, -1):
  b_re = b[(len(b)-i):].replace('?',"[a-z]")
  pattern = re.compile(r"%s" % b_re)
  res = pattern.search(c[:i+1])
  if res is not None and res.group() != "":
    _b[1][0] = i
    _c[1][1] = i
    break

for i in range(min(len(b), len(a)), 0, -1):
  b_re = b[(len(b)-i):].replace('?',"[a-z]")
  pattern = re.compile(r"%s" % b_re)
  res = pattern.search(a[:i+1])
  if res is not None and res.group() != "":
    _b[0][0] = i
    _a[0][1] = i
    break

for i in range(min(len(c), len(a)), 0, -1):
  c_re = c[(len(c)-i):].replace('?',"[a-z]")
  pattern = re.compile(r"%s" % c_re)
  res = pattern.search(a[:i+1])
  if res is not None and res.group() != "":
    _c[1][0] = i
    _a[0][1] = i
    break

for i in range(min(len(c), len(b)), 0, -1):
  c_re = c[(len(c)-i):].replace('?',"[a-z]")
  pattern = re.compile(r"%s" % c_re)
  res = pattern.search(b[:i+1])
  if res is not None and res.group() != "":
    _c[1][0] = i
    _b[1][1] = i
    break

common_length = max(_a[0][1] + _a[1][0], _a[0][0] + _a[1][1], _b[0][1] + _b[1][0], _b[0][0] + _b[1][1], _c[0][1] + _c[1][0], _c[0][0] + _c[1][1])

print(len(a) + len(b) + len(c) - common_length)

Submission status: Wrong Answer

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

def eq(x, y):
    if len(x) != len(y):
        return False
    for v1, v2 in zip(x, y):
        if v1 == "?" or v2 == "?":
            continue
        if v1 != v2:
            return False
    return True

def match(A, B):
    for i in reversed(range(len(A) + 1)):
        if eq(A[-i:], B[:i]):
            return i
    return 0

def merge(A, B, i):
    if i == 0:
        return A + B
    r = ""
    for v1, v2 in zip(A[-i:], B[:i]):
        if v1 == v2:
            r += v1
        elif v1 == "?":
            r += v2
        else:
            r += v1

    return A[:-i] + r + B[i:]

import itertools
minv = float('inf')
value = None
for v in itertools.permutations([a, b, c], r=3):
    A, B, C = v
    s = merge(A, B, match(A, B))
    s = merge(s, C, match(s, C))
    l = len(s)
    if minv >= l:
        minv = l
        value = s
print(minv)

Submission status: Wrong Answer

Solution code:
s = []; ans = 0
for _ in range(3):
    s.append(input())
for x in s:
    ans += len(x)
a = [[0,1,2],[0,2,1],[1,0,2],[1,2,0],[2,1,0],[2,0,1]]
def check(s,t):
    for i in range(len(s)):
        k = i; j = 0;
        while k < len(s) and j < len(t):
            if s[k]=='?' or t[j]=='?' or s[k]==t[j]:
                k += 1; j += 1;
            else:
                break
        if k == len(s):
            return s[:i] + t
        if j == len(t):
            return s
    return s + t
for b in a:
    ans = min(ans,len(check(check(s[b[0]],s[b[1]]),s[b[2]])))
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return input().rstrip()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("inf")

#solve
def solve():
    a = S()
    b = S()
    c = S()
    def f(a, b, c):
        lena, lenb, lenc = len(a), len(b), len(c)
        dpab = [inf] * (lena + 1)
        dpbc = [inf] * (lenb + 1)
        dpac = [inf] * (lena + 1)
        for l in range(lena + 1):
            f = 0
            tl = l
            for r in range(lenb):
                if l == lena:
                    break
                if a[l] == b[r] or a[l] == "?" or b[r] == "?":
                    l += 1
                    continue
                else:
                    f = 1
                    break
            if f:
                continue
            dpab[tl] = max(tl + lenb, lena)

        for l in range(lenb + 1):
            f = 0
            tl = l
            for r in range(lenc):
                if l == lenb:
                    break
                if b[l] == c[r] or b[l] == "?" or c[r] == "?":
                    l += 1
                    continue
                else:
                    f = 1
                    break
            if f:
                continue
            dpbc[tl] = max(tl + lenc, lenb)

        for l in range(lena + 1):
            f = 0
            tl = l
            for r in range(lenc):
                if l == lena:
                    break
                if a[l] == c[r] or a[l] == "?" or c[r] == "?":
                    l += 1
                    continue
                else:
                    f = 1
                    break
            if f:
                continue
            dpac[tl] = max(tl + lenc, lena)

        res = lena + lenb + lenc
        for l in range(lena + 1):
            for r in range(lenb + 1):
                lr = l + r
                if dpab[l] != inf and dpbc[r] != inf:
                    if lr >= lena:
                        res = min(l + dpbc[r], res)
                    else:
                        if dpac[lr] != inf:
                            if dpbc[r] == lenb:
                                res = min(res, dpab[l])
                            else:
                                res = min(res, dpac[lr])
        return res
    ans = len(a) + len(b) + len(c)
    ans = min(ans, f(a, b, c))
    ans = min(ans, f(a, c, b))
    ans = min(ans, f(b, a, c))
    ans = min(ans, f(b, c, a))
    ans = min(ans, f(c, b, a))
    ans = min(ans, f(c, a, b))
    print(ans)

    return


#main
if __name__ == '__main__':
    solve()