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
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 = B, C, A

    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:
            continue
        elif m == len(C):
            ans = min(ans, len(r))
        else:
            ans = min(ans, len(r)+N-m)

print(ans)

Submission status: Wrong Answer

Solution code:
def check(x,y,m):
  for i in range(m):
    if x[len(x)-m+i]!=y[i] and x[len(x)-m+i]!='?' and y[i]!='?':
      return False
  return True
def f(x,y):
  s1=x+y
  for i in range(1,min(len(x),len(y))+1):
    if check(x,y,i):
      s1=x[:len(x)-i]+y
  return s1
a=input()
b=input()
c=input()
l=[f(f(a,b),c),f(f(b,a),c),f(f(a,c),b),f(f(c,a),b),f(f(b,c),a),f(f(c,b),a),f(a,f(b,c)),f(a,f(c,b)),f(b,f(a,c)),f(b,f(c,a)),f(c,f(a,b)),f(c,f(b,a))]
ans=10**18
for i in range(len(l)):
  ans=min(ans,len(l[i]))
print(ans)

Submission status: Wrong Answer

Solution code:
a,b,c = sorted([input(),input(),input()],key=lambda x: -len(x))
A,B,C =a,b,c
# 中に入るか、外のどこに出るかだけを考えれば良い

## 真ん中を調べる(これで入るなら両端を調べる必要はない)

for i in range(len(a)-len(b)+1):
  for cha,chb in zip( b, a[i:i+len(b)]):
    if cha == "?" or chb == "?" or cha == chb:
      continue
    else:
      break
  else:
    res = {a}
    break   
else:
  res = set([a+b,b+a])
  ## 左
  for i in range(1,len(b)):
    ## aの最初から、bの末尾までが同じかどうかを確かめていく
    for cha,chb in zip( b[-i:], a[:i]):
      if cha == "?" or chb == "?" or cha == chb:
        continue
      else:
        break
    else:
      res |= {b +a[i:]}
    ## 逆方向
    for cha,chb in zip( b[:i], a[-i:]):
      if cha == "?" or chb == "?" or cha == chb:
        continue
      else:
        break
    else:
      res |= {a+b[i:]}
# print(res)
minlen = 6000
for a in res:
  b = c
  for i in range(len(a)-len(b)+1):
    for cha,chb in zip( b, a[i:i+len(b)]):
      if cha == "?" or chb == "?" or cha == chb:
        continue
      else:
        break
    else:
      minlen = min(len(a),minlen)
      break   
  else:
    res = set([a+b,b+a])
    ## 左
    for i in range(1,len(b)):
      ## aの最初から、bの末尾までが同じかどうかを確かめていく
      for cha,chb in zip( b[-i:], a[:i]):
        if cha == "?" or chb == "?" or cha == chb:
          continue
        else:
          break
      else:
        res |= {b +a[i:]}
      ## 逆方向
      for cha,chb in zip( b[:i], a[-i:]):
        if cha == "?" or chb == "?" or cha == chb:
          continue
        else:
          break
      else:
        res |= {a+b[i:]}
    minlen = min(min(map(len,res)),minlen)

###
a,b,c = A,C,B

for i in range(len(a)-len(b)+1):
  for cha,chb in zip( b, a[i:i+len(b)]):
    if cha == "?" or chb == "?" or cha == chb:
      continue
    else:
      break
  else:
    res = {a}
    break   
else:
  res = set([a+b,b+a])
  ## 左
  for i in range(1,len(b)):
    ## aの最初から、bの末尾までが同じかどうかを確かめていく
    for cha,chb in zip( b[-i:], a[:i]):
      if cha == "?" or chb == "?" or cha == chb:
        continue
      else:
        break
    else:
      res |= {b +a[i:]}
    ## 逆方向
    for cha,chb in zip( b[:i], a[-i:]):
      if cha == "?" or chb == "?" or cha == chb:
        continue
      else:
        break
    else:
      res |= {a+b[i:]}
# print(res)
for a in res:
  b = c
  for i in range(len(a)-len(b)+1):
    for cha,chb in zip( b, a[i:i+len(b)]):
      if cha == "?" or chb == "?" or cha == chb:
        continue
      else:
        break
    else:
      minlen = min(len(a),minlen)
      break   
  else:
    res = set([a+b,b+a])
    ## 左
    for i in range(1,len(b)):
      ## aの最初から、bの末尾までが同じかどうかを確かめていく
      for cha,chb in zip( b[-i:], a[:i]):
        if cha == "?" or chb == "?" or cha == chb:
          continue
        else:
          break
      else:
        res |= {b +a[i:]}
      ## 逆方向
      for cha,chb in zip( b[:i], a[-i:]):
        if cha == "?" or chb == "?" or cha == chb:
          continue
        else:
          break
      else:
        res |= {a+b[i:]}
    minlen = min(min(map(len,res)),minlen)
print(minlen)

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def LCS(s,t):
    global dp,ns,nt
    ns=len(s)
    nt=len(t)
    dp=[[0 for _ in range(nt+1)] for _ in range(ns+1)]
    for i in range(1,ns+1):
        for j in range(1,nt+1):
            if s[i-1]=='?' or t[j-1]=='?' or s[i-1]==t[j-1]:
                dp[i][j]=dp[i-1][j-1]+1
            else:
                dp[i][j]=max( dp[i][j-1] , dp[i-1][j] )
    return dp[ns][nt]
def recovery(s,t):
    f = LCS(s,t)
    x=ns
    y=nt
    ans=[]
    xr,yr = -1,-1
    xl,yl = -1,-1
    while x>0 and y>0:
        if dp[x][y]==dp[x][y-1]:
            y-=1
        elif dp[x][y]==dp[x-1][y]:
            x-=1
        else:
            if xr == -1:
                xr = x
                yr = y
            x-=1
            y-=1
            xl = x
            yl = y
            ans.append(s[x])
    ans.reverse()
    return xr,xl,yr,yl,f
def LCS1(s,t):
    global dp,ns,nt
    ns=len(s)
    nt=len(t)
    dp=[[0 for _ in range(nt+1)] for _ in range(ns+1)]
    for i in range(1,ns+1):
        for j in range(1,nt+1):
            if s[i-1]==t[j-1]:
                dp[i][j]=dp[i-1][j-1]+1
            else:
                dp[i][j]=max( dp[i][j-1] , dp[i-1][j] )
    return dp[ns][nt]
def recovery1(s,t):
    f = LCS1(s,t)
    x=ns
    y=nt
    ans=[]
    xr,yr = -1,-1
    xl,yl = -1,-1
    while x>0 and y>0:
        if dp[x][y]==dp[x][y-1]:
            y-=1
        elif dp[x][y]==dp[x-1][y]:
            x-=1
        else:
            if xr == -1:
                xr = x
                yr = y
            x-=1
            y-=1
            xl = x
            yl = y
            ans.append(s[x])
    ans.reverse()
    return xr,xl,yr,yl,f

def main():
    a = input()
    b = input()
    c = input()
    xr1,xl1,yr1,yl1,f1 = recovery(a,b)
    xr2,xl2,yr2,yl2,f2 = recovery(b,c)
    xr3,xl3,yr3,yl3,f3 = recovery(a,c)
    na = len(a)
    nb = len(b)
    nc = len(c)
    ans = [na+nb+nc for _ in range(6)]
    if xr1 == na and yl1 == 0: #ab
        ans[0] -= f1
    if xr2 == nb and yl2 == 0: #bc
        ans[0] -= f2

    if xr3 == na and yl3 == 0: #ac
        ans[1] -= f3
    if xl2 == 0 and yr2 == nc: #cb
        ans[1] -= f2

    if xl1 == 0 and yr1 == nb: #ba
        ans[2] -= f1
    if xr3 == na and yl3 == 0: #ac
        ans[2] -= f3

    if xr2 == nb and yl2 == 0: #bc
        ans[3] -= f2
    if xl3 == 0 and yr3 == nc: #ca
        ans[3] -= f3

    if xl3 == 0 and yr3 == nc: #ca
        ans[4] -= f3
    if xr1 == na and yl1 == 0: #ab
        ans[4] -= f1

    if xl2 == 0 and yr2 == nc: #cb
        ans[5] -= f2
    if xl1 == 0 and yr1 == nb: #ba
        ans[5] -= f1
    ans_A = min(ans)

    xr1,xl1,yr1,yl1,f1 = recovery1(a,b)
    xr2,xl2,yr2,yl2,f2 = recovery1(b,c)
    xr3,xl3,yr3,yl3,f3 = recovery1(a,c)
    ans = [na+nb+nc for _ in range(6)]
    if xr1 == na and yl1 == 0: #ab
        ans[0] -= f1
    if xr2 == nb and yl2 == 0: #bc
        ans[0] -= f2

    if xr3 == na and yl3 == 0: #ac
        ans[1] -= f3
    if xl2 == 0 and yr2 == nc: #cb
        ans[1] -= f2

    if xl1 == 0 and yr1 == nb: #ba
        ans[2] -= f1
    if xr3 == na and yl3 == 0: #ac
        ans[2] -= f3

    if xr2 == nb and yl2 == 0: #bc
        ans[3] -= f2
    if xl3 == 0 and yr3 == nc: #ca
        ans[3] -= f3

    if xl3 == 0 and yr3 == nc: #ca
        ans[4] -= f3
    if xr1 == na and yl1 == 0: #ab
        ans[4] -= f1

    if xl2 == 0 and yr2 == nc: #cb
        ans[5] -= f2
    if xl1 == 0 and yr1 == nb: #ba
        ans[5] -= f1
    ans_B = min(ans)
    print(min(ans_A,ans_B))

if __name__ == "__main__":
    main()

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))))

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):
                ab = -1
                break
            else:
                continue
    if ab >= 0:
        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):
                    bc = -1
                    break
                else:
                    continue
    else:
        for i in range(len(a)):
            if a[i] == c[0] or a[i] == "?" or c[0] == "?":
                count = 1
                for j in range(1, min(len(a) - i, len(c))):
                    if a[i+j] == c[j] or a[i+j] == "?" or c[j] == "?":
                        count += 1
                    else:
                        break
                if count + i == len(a):
                    bc = i
                    break
                elif count == len(c):
                    bc = -1
                    break
                else:
                    continue
    tmp = 0
    if ab >= 0 and bc >= 0:
        tmp = ab + bc + len(c)
    elif ab >= 0 and bc < 0:
        tmp = len(b) + ab
    elif ab < 0 and bc >= 0:
        tmp = bc + len(c)
    else:
        tmp = len(a)
    ans = min(ans, tmp)

print(ans)

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 = B, C, A

    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:
            continue
        ans = min(ans, len(r)+N-m)

print(ans)

Submission status: Wrong Answer

Solution code:
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
import copy

def main():
    mod=10**9+7
    
    #sとtの被りを調べ，合体した文字列を返す，listで受け取る
    def make(s,t):
        ns=len(s)
        nt=len(t)
        temp=ns#何文字目から結合するか
        for i in range(ns):#sのi文字目からスタート
            flag=1
            for j in range(nt):#tのj文字目を見る
                if i+j>=ns:
                    break
                if s[i+j]!=t[j]:
                    if s[i+j]=="?" or t[j]=="?":
                        continue
                    else:
                        flag=0
                        break
            if flag==1:
                temp=i
                break
            
        ss=copy.deepcopy(s)
        for i in range(nt):
            if i+temp>=ns:
                break
            if s[temp+i]=="?":
                ss[temp+i]=t[i]
                
        ss+=t[ns-temp:]
        #print(s,t,ss)
        return ss
    
        
    
    a=list(input())
    b=list(input())
    c=list(input())
    
    
    ans=[]
    ans.append(make(make(a,b),c))
    ans.append(make(make(b,a),c))
    ans.append(make(c,make(a,b)))
    ans.append(make(c,make(b,a)))
    
    ans.append(make(make(a,c),b))
    ans.append(make(make(c,a),b))
    ans.append(make(b,make(a,c)))
    ans.append(make(b,make(c,a)))
    
    ans.append(make(make(b,c),a))
    ans.append(make(make(c,b),a))
    ans.append(make(a,make(b,c)))
    ans.append(make(a,make(c,b)))
    
    
    ansn=10**10
    for i in range(len(ans)):
        ansn=min(ansn,len(ans[i]))
                 
    print(ansn)
        
    
            
            
        
            
            
        
            
        
        

main()

Submission status: Wrong Answer

Solution code:
def search(w1, w2):
	i = p = 0
	n1 = len(w1)
	n2 = len(w2)
	while i < n1 and p < n2:
		if w1[i] == w2[p] or w1[i] == "?" or w2[p] == "?":
			i += 1
			p += 1
		elif p == 0:
			i += 1
		else:
			p = 0
		if i == n1:
			return p,i
	return p,i

from itertools import permutations
a=input()
b=input()
c=input()
s=len(a)+len(b)+len(c)
ans=1e9
for n1,n2,n3 in permutations((a,b,c),3):
	x,ii=search(n1,n2)
	t=list(n1)
	tt=list(n2)
	if x:
		for i in range(x):
			if not t[ii-x+i]=="?":
				tt[i]=t[ii-x+i]
	tt="".join(tt)
	y,_=search(tt,n3)
	ans=min(ans,s-x-y)
	# print(n1,n2,n3,tt,x,y,s-x-y)
print(ans)

Submission status: Wrong Answer

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

def check(left, right):
    for i in range(len(left)):
        if(left[i] != "?" and right[i] != "?" and left[i] != right[i]):
            return False
    return True

def merge(left, right):
    ans = ""
    for i in range(len(left)):
        if(left[i] == "?"):
            ans += right[i]
        else:
            ans += left[i]
    return ans

def connect(left, right):
    for i in range(len(left)):
        last_index = min(len(left), i + len(right))
        if(check(left[i:last_index], right[:last_index]) == True):
            if(last_index != len(left)):
                return left
            return left[:i] + merge(left[i:last_index], right[:last_index-i]) + right[last_index-i:]
    return left + right    

ansList = []
ansList.append(connect(a, connect(b, c)))
ansList.append(connect(a, connect(c, b)))
ansList.append(connect(b, connect(a, c)))
ansList.append(connect(b, connect(c, a)))
ansList.append(connect(c, connect(a, b)))
ansList.append(connect(c, connect(b, a)))

minAns = 99999
for ans in ansList:
    minAns = min(minAns, len(ans))
print(minAns)

Submission status: Wrong Answer

Solution code:
a = list(input())
b = list(input())
c = list(input())
n0 = len(a)
n1 = len(b)
n2 = len(c)
al = []
def mae(a,b):
    n0 = len(a)
    n1 = len(b)
    al = a+b
    now = []
    count = 0
    for i in range(n1):
        if a[0] == b[i] or a[0] == "?" or b[i] == "?":
            now2 = now
            count2 = 0
            for j in range(min(n1-i,n0)):
                if a[j] != b[j+i] and a[j] != "?" and b[j+i] != "?":
                    count2 = 0
                    break
                else:
                    count2 += 1
                    now2.append(a[j])
            if count2 > count:
                al = now2
                count = count2
        now.append(b[i])
    return al,count
def ushiro(a,b):
    n0 = len(a)
    n1 = len(b)
    al = a+b
    now = []
    count = 0
    for i in range(n1-1,-1,-1):
        if a[n0-1] == b[i] or a[n0-1] == "?" or b[i] == "?":
            now2 = now
            count2 = 0
            for j in range(min(i,n0)):
                if a[n0-1-j] != b[i-j] and a[n0-1-j] != "?" and b[i-j] != "?":
                    count2 = 0
                    break
                else:
                    count2 += 1
                    now2.append(a[j])
            if count2 > count:
                count = count2
                al = now2
        now.append(b[i])
    return al,count
ansl = [0]
c1,k = mae(a,b)
c2,p = ushiro(a,b)
al1,k1 = mae(c1,c)
al2,p1 = ushiro(c2,c)
ansl.append(k+k1)
ansl.append(p+p1)
al1,k1 = mae(c1,c)
al2,p1 = ushiro(c2,c)
ansl.append(k+k1)
ansl.append(p+p1)
c1,k = mae(b,a)
c2,p = ushiro(b,a)
al1,k1 = mae(c1,c)
al2,p1 = ushiro(c2,c)
ansl.append(k+k1)
ansl.append(p+p1)
c1,k = mae(c,b)
c2,p = ushiro(c,b)
al1,k1 = mae(c1,a)
al2,p1 = ushiro(c2,a)
ansl.append(k+k1)
ansl.append(p+p1)
c1,k = mae(b,c)
c2,p = ushiro(b,c)
al1,k1 = mae(c1,a)
al2,p1 = ushiro(c2,a)
ansl.append(k+k1)
ansl.append(p+p1)
c1,k = mae(c,a)
c2,p = ushiro(c,a)
al1,k1 = mae(c1,b)
al2,p1 = ushiro(c2,b)
ansl.append(k+k1)
ansl.append(p+p1)
c1,k = mae(a,c)
c2,p = ushiro(a,c)
al1,k1 = mae(c1,b)
al2,p1 = ushiro(c2,b)
ansl.append(k+k1)
ansl.append(p+p1)
print(n0+n1+n2-max(ansl))

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:
            return max(0, q - p + i)
    return q


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

res = []
res.append(p + r1 + r4)
res.append(p + r2 + r6)
res.append(q + r3 + r2)
res.append(q + r4 + r5)
res.append(r + r5 + r1)
res.append(r + r6 + r3)
if r1 == 0:
    res.append(p + r2)
if r2 == 0:
    res.append(p + r1)
if r3 == 0:
    res.append(q + r4)
if r4 == 0:
    res.append(q + r3)
if r5 == 0:
    res.append(r + r6)
if r6 == 0:
    res.append(r + r5)
print(min(res))

Submission status: Wrong Answer

Solution code:
import sys
import itertools
input = sys.stdin.readline
sys.setrecursionlimit(100000)


def read_values():
    return map(int, input().split())


def read_index():
    return map(lambda x: x - 1, map(int, input().split()))


def read_list():
    return list(read_values())


def read_lists(N):
    return [read_list() for n in range(N)]


def functional(N, mod):
    F = [1] * (N + 1)
    for i in range(N):
        F[i + 1] = (i + 1) * F[i] % mod
    return F


def make_index(A, B):
    I = []
    for i in range(len(A)):
        for j in range(min(len(B), len(A) - i)):
            if A[i + j] == "?":
                continue

            if B[j] == "?":
                continue

            if A[i + j] != B[j]:
                break
        else:
            I.append(i)
        
    I.append(len(A))
    return I


def check(AB, BC, AC, len_c):
    import bisect

    res = 10 ** 20
    for i in AB:
        for j in BC:
            if i + j >= AB[-1]:
                res = min(res, i + max(j + len_c, BC[-1]))
                continue

            t = bisect.bisect_left(AC, i + j) 
            if AC[t] != i + j:
                continue
            res = min(res, max(AB[-1], i + BC[-1], i + j + len_c))
    return res


def main():
    A = input().strip()
    B = input().strip()
    C = input().strip()
    AB = make_index(A, B)
    BA = make_index(B, A)
    BC = make_index(B, C)
    CB = make_index(C, B)
    CA = make_index(C, A)
    AC = make_index(A, C)

    res = 10 ** 20
    res = min(
        check(AB, BC, AC, len(C)), 
        check(BA, AC, BC, len(C)), 
        check(BC, CA, BA, len(A)), 
        check(CB, BA, CA, len(A)), 
        check(CA, AB, CB, len(B)), 
        check(AC, CB, AB, len(B))
        )
    print(res)


if __name__ == "__main__":
    main()

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 = inf
        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 dpab[lr] != inf:
                            if dpbc[r] == lenb:
                                res = min(res, dpab[l])
                            else:
                                res = min(res, dpac[lr])
        # print(res)
        # print(dpab)
        # print(dpac)
        # print(dpbc)
        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)



        # f = 0
        # bc = b + c
        # ab = a + b
        # lenab, lenbc = len(ab), len(bc)
        # tl = lena
        # tr = lenc
        # for l in range(lena):
        #     for k in range(lenbc):
        #         if l == lena:
        #             break
        #         if a[l] == bc[k] or a[l] == "?" or bc[k] == "?":
        #             l += 1
        #             continue
        #         else:
        #             f = 1
        #             break
        #     if f:
        #         continue
        #     tl = l
        #     break
        # for r in range(lenab):
        #     for k in range(lenc):
        #         if r == lenb:
        #             break
        #         if b[r] == c[k] or b[r] == "?" or c[k] == "?":
        #             r += 1
        #             continue
        #         else:
        #             f = 1
        #             break
        #         if f:
        #             continue
        #     tr = r
            # break

        # return 

    return


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

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

    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)