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:
def examA():
    L = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
    K = I()

    ans = L[K-1]
    print(ans)
    return

def examB():
    H, W = LI()
    if H==1 or W==1:
        print(1)
        return
    ans = H*W//2 + (H*W)%2
    print(ans)
    return

def examC():
    a,b,c = LI()
    if c-(a+b)<=0:
        print("No")
        return
    judge = (c-a-b)**2-4*a*b
    if judge>0:
        print("Yes")
    else:
        print("No")
    return

def examD():
    def bfs(N):
        W = [0]*(5*10**6)
        L = [""]*(5*10**6)
        que = deque()
        que.append(0)
        L[0] = "a"
        W[0] = 0
        flag = True
        ne = 0
        while(flag):
            now = que.popleft()
            nowW = W[now]
            if len(L[now])==N:
                flag = False
                break
            for i in range(W[now]+2):
                ne += 1
                W[ne] = max(nowW,i)
                que.append(ne)
                L[ne] = L[now] + alphabet[i]
        return L
    N = I()
    D = bfs(N)
    for ans in D:
        if not ans:
            break
        if len(ans)<N:
            continue
        print(ans)
    return

def examE():
    def LCS_R(S,T):
        N = len(S); M = len(T)
        dp = [[inf] * (M + 1) for _ in range(N + 1)]
        dp_S = [[""] * (M + 1) for _ in range(N + 1)]
        dp[0][0] = 0
        for i in range(N):
            for j in range(M):
                if S[i] == T[j] or S[i] == "?" or T[j] == "?":
                    dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + 1)
                    if dp[i + 1][j + 1]==dp[i][j] + 1:
                        cur = S[i]
                        if S[i] == "?" or T[j] == "?":
                            cur = "?"
                        dp_S[i+1][j+1] = dp_S[i][j] + cur
                dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]+1)
                dp[i][j+1] = min(dp[i][j+1], dp[i][j] + 1)
                if dp[i + 1][j] == dp[i][j] + 1:
                    dp_S[i + 1][j] = dp_S[i][j] + S[i]
                if dp[i][j + 1] == dp[i][j] + 1:
                    dp_S[i][j + 1] = dp_S[i][j] + T[j]
        dp[N][M] = min(dp[N][M], dp[N-1][M] + 1)
        dp[N][M] = min(dp[N][M], dp[N][M-1] + 1)
        if dp[N][M] == dp[N-1][M] + 1:
            dp_S[N][M] = dp_S[N-1][M] + S[N-1]
        if dp[N][M] == dp[N][M-1] + 1:
            dp_S[N][M] = dp_S[N][M-1] + T[M-1]
        return dp,dp_S

    A = SI()
    B = SI()
    C = SI()

    a1,d1 = LCS_R(A,B)
    a2,d2 = LCS_R(B,C)
    a3,d3 = LCS_R(C,A)

    S1,d1 = LCS_R(d1[-1][-1],C)
    S2,d2 = LCS_R(d2[-1][-1],A)
    S3,d3 = LCS_R(d3[-1][-1],B)

    ans = min(S1[-1][-1],S2[-1][-1],S3[-1][-1])
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examE()

"""

"""

Submission status: Wrong Answer

Solution code:
def match(s1, s2, ind):
	j=0
	i=ind
	while(i<len(s1)):
		if s1[i]!='?' and s2[j]!='?' and s1[i]!=s2[j]:
			return False
		i+=1
		j+=1
	return True

def fusion(s1, s2):
	ind = len(s1)
	for i in range(len(s1)):
		if len(s1)-i <= len(s2):
			if match(s1, s2, i):
				ind = i
				break
	# ~ res = s1[:ind]+s2
	res = s1[:ind]
	j=0
	i=ind
	while(i<len(s1) or j<len(s2)):
		if i<len(s1) and s1[i]!='?':
			res+=s1[i]
		elif j<len(s2) and s2[j]!='?':
			res+=s2[j]
		else: res+='?'
		i+=1
		j+=1
	# ~ print(s1,"+",s2,"=",res)
	return res

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

f1 = fusion( fusion(a,b), c)
f2 = fusion( fusion(a,c), b)
f3 = fusion( fusion(b,a), c)
f4 = fusion( fusion(b,c), a)
f5 = fusion( fusion(c,a), b)
f6 = fusion( fusion(c,b), a)
# ~ print("f1=",f1)
# ~ print("f2=",f2)
# ~ print("f3=",f3)
# ~ print("f4=",f4)
# ~ print("f5=",f5)
# ~ print("f6=",f6)

ans = min(len(f1),len(f2),len(f3),len(f4),len(f5),len(f6))

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python


def issame(s1, s2):
    # print(s1, s2)
    for i in range(len(s1)):
        if s1[i] == s2[i] or '?' in (s1[i], s2[i]):
            continue
        return False
    return True


def isinclude(s1, s2):
    if len(s1) > len(s2):
        return False
        # return isinclude(s2, s1)
    n = len(s2) - len(s1) + 1
    for l in range(n):
        if issame(s1, s2[l:len(s1) + l]):
            return True
    return False


def same_len(s1, s2):
    if isinclude(s1, s2):
        return min(len(s1), len(s2))
    n = min(len(s1), len(s2))
    for l in range(n):
        ll = n - l
        # print(ll, s1[-4:], s1[-ll:])
        if issame(s1[-ll:], s2[:ll]):
            return ll
    else:
        return 0


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

n = len(a) + len(b) + len(c)
if isinclude(a, b) or isinclude(a, c):
    cut = [same_len(b, c), same_len(c, b)]
    n -= len(a)
elif isinclude(b, a) or isinclude(b, c):
    cut = [same_len(a, c), same_len(c, a)]
    n -= len(b)
elif isinclude(c, a) or isinclude(c, b):
    cut = [same_len(a, b), same_len(b, a)]
    n -= len(c)
else:
    cut = [
        same_len(a, b) + same_len(b, c),
        same_len(a, c) + same_len(c, b),
        same_len(b, a) + same_len(a, c),
        same_len(b, c) + same_len(c, a),
        same_len(c, a) + same_len(a, b),
        same_len(c, b) + same_len(b, a),
    ]
# print(cut)

print(n - max(cut))

Submission status: Wrong Answer

Solution code:
string = list(input("Enter a string: "))
lists = []
count = 0
for x in string:
  if x not in lists:
    lists.append(x)
    count += 1
print(count)

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:
            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:
            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 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+t[ii:])
	y,_=search(tt,n3)
	ans=min(ans,s-x-y)
	# print(n1,n2,n3,x,y,ii,s-x-y)
print(ans)

Submission status: Wrong Answer

Solution code:
def z_algo(S):
    N = len(S)
    A = [0]*N
    i = 1; j = 0
    A[0] = l = len(S)
    while i < l:
        while i+j < l and (S[j] == S[i+j] or S[i] == '?' or S[i+j] == '?'):
            j += 1
        if not j:
            i += 1
            continue
        A[i] = j
        k = 1
        while l-i > k < j - A[k]:
            A[i+k] = A[k]
            k += 1
        i += k; j -= k
    return A

def merge(a, b, i):
    s = b[:i]
    for aa, bb in zip(b[i:], a):
        if aa == '?':
            s += bb
        else:
            s += aa
    s += a[len(b) - i:]
    return s

def concat(a, b):
    AB = z_algo(a + b)
    BA = z_algo(b + a)
    abm, abi = 0, len(b)
    for i, k in enumerate(AB[len(a):]):
        if k == len(b) - i:
            abm, abi = k, i
            break
    bam, bai = 0, len(a)
    for i, k in enumerate(BA[len(b):]):
        if k  == len(a) - i:
            bam, bai = k, i
    if abm > bam:
        return merge(a, b, abi)
    else:
        return merge(b, a, bai)

def main():
    a, b, c = input(), input(), input()
    ab = concat(a, b)
    ac = concat(a, c)
    ca = concat(c, a)
    cb = concat(c, b)

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

    return min(len(abc), len(acb), len(bac), len(bca), len(cab), len(cba))

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
a=input()
b=input()
c=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

#(a,b),c
match=0
for i in range(1,min(len(a),len(b))+1):
  sub1=a[-i:]
  sub2=b[:i]
  #print(sub1,sub2)
  if eq_wc(sub1,sub2):
    match+=1
  else:
    break
ab=a+b[match:]
match=0
for i in range(1,min(len(ab),len(c))+1):
  sub1=ab[-i:]
  sub2=c[:i]
  #print(sub1,sub2)
  if eq_wc(sub1,sub2):
    match+=1
  else:
    break
abc=ab+c[match:]
#print(len(abc),abc)

#(a,c),b
match=0
for i in range(1,min(len(a),len(c))+1):
  sub1=a[-i:]
  sub2=c[:i]
  #print(sub1,sub2)
  if eq_wc(sub1,sub2):
    match+=1
  else:
    break
ac=a+c[match:]
match=0
for i in range(1,min(len(ac),len(b))+1):
  sub1=ac[-i:]
  sub2=b[:i]
  #print(sub1,sub2)
  if eq_wc(sub1,sub2):
    match+=1
  else:
    break
acb=ac+b[match:]
#print(len(acb),acb)

#(b,a),c
match=0
for i in range(1,min(len(b),len(a))+1):
  sub1=b[-i:]
  sub2=a[:i]
  #print(sub1,sub2)
  if eq_wc(sub1,sub2):
    match+=1
  else:
    break
ba=b+a[match:]
match=0
for i in range(1,min(len(ba),len(c))+1):
  sub1=ba[-i:]
  sub2=c[:i]
  #print(sub1,sub2)
  if eq_wc(sub1,sub2):
    match+=1
  else:
    break
bac=ba+c[match:]
#print(len(bac),bac)

#(b,c),a
match=0
for i in range(1,min(len(b),len(c))+1):
  sub1=b[-i:]
  sub2=c[:i]
  #print(sub1,sub2)
  if eq_wc(sub1,sub2):
    match+=1
  else:
    break
bc=b+c[match:]
match=0
for i in range(1,min(len(bc),len(a))+1):
  sub1=bc[-i:]
  sub2=a[:i]
  #print(sub1,sub2)
  if eq_wc(sub1,sub2):
    match+=1
  else:
    break
bca=bc+a[match:]
#print(len(bca),bca)

#(c,a),b
match=0
for i in range(1,min(len(c),len(a))+1):
  sub1=c[-i:]
  sub2=a[:i]
  #print(sub1,sub2)
  if eq_wc(sub1,sub2):
    match+=1
  else:
    break
ca=c+a[match:]
match=0
for i in range(1,min(len(ca),len(b))+1):
  sub1=ca[-i:]
  sub2=b[:i]
  #print(sub1,sub2)
  if eq_wc(sub1,sub2):
    match+=1
  else:
    break
cab=ca+b[match:]
#print(len(cab),cab)

#(c,b),a
match=0
for i in range(1,min(len(c),len(b))+1):
  sub1=c[-i:]
  sub2=b[:i]
  #print(sub1,sub2)
  if eq_wc(sub1,sub2):
    match+=1
  else:
    break
cb=c+b[match:]
match=0
for i in range(1,min(len(cb),len(a))+1):
  sub1=cb[-i:]
  sub2=a[:i]
  #print(sub1,sub2)
  if eq_wc(sub1,sub2):
    match+=1
  else:
    break
cba=cb+a[match:]
#print(len(cba),cba)

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

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))
    #print(abc-max(p1,p2,p3,p4,p5,p6))
    print(abc-bbb-ccc)
    return

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

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:
            ans = min(ans, len(r)+len(C))
        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:
from itertools import permutations
a = list(input())
b = list(input())
c = list(input())
ans = len(a)+len(b)+len(c)
l = len(a)+len(b)+len(c)
for X, Y, Z in permutations((a, b, c), 3):
  #XY
  sameXY = min(len(X), len(Y))
  while sameXY > 0:
    ok = True
    i = 0
    while i < sameXY:
      if X[-(sameXY-i)] != Y[i] and X[-(sameXY-i)] != "?" and Y[i] != "?":
        ok = False
        break
      i += 1
    if ok:
      break
    sameXY -= 1
  #YZ
  Y = X+Y[sameXY:]
  sameYZ =  min(len(Y), len(Z))
  while sameYZ > 0:
    ok = True
    i = 0
    while i < sameYZ:
      if Y[-(sameYZ-i)] != Z[i] and Y[-(sameYZ-i)] != "?" and Z[i] != "?":
        ok = False
        break
      i += 1
    if ok:
      break
    sameYZ -= 1
  ans = min(ans, l-sameXY-sameYZ)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
a = input().strip()
b = input().strip()
c = input().strip()

def match(x, y):
  xl = len(x)
  yl = len(y)
  N = min(xl, yl)
  n = N
  m = 0
  while n > 0:
    #print(n)
    for j in range(n):
      #print(x[xl-n+j], y[j])
      if x[xl-n+j] != '?' and y[j] != '?' and x[xl-n+j] != y[j]:
        break
    else:
      m = n
      break
    n -= 1
  #print(x, y, m)
  return m

sents = [a, b, c]
orders = [(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)]
max_match = 0
for o in orders:
  for i in range(2):
    m = 0
    m += match(sents[o[i]], sents[o[i+1]])
    m += match(sents[o[i]], sents[o[i+1]])
    max_match = max(max_match, m)

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

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
    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:
a=str(input())
b=str(input())
c=str(input())
ans = 10**9
ab=""
cnt = 0
while ab=="" and cnt<len(a):
  for i in range(min(len(b),len(a)-cnt)):
    if a[i+cnt]!="?" and b[i]!="?" and a[i+cnt]!=b[i]:
      ab=""
      break
    else:
      if a[i+cnt]!="?":
        ab += a[i+cnt]
      else:
        ab +=b[i]
if ab=="":
  ab =a+b
abc=""
cnt=0
while abc=="" and cnt<len(ab):
  for i in range(min(len(c),len(ab)-cnt)):
    if ab[i+cnt]!="?" and c[i+cnt]!="?" and ab[i+cnt]!=c[i+cnt]:
      abc=""
      break
    else:
      if ab[i+cnt]!="?":
        abc+=ab[i+cnt]
      else:
        abc+=c[i]
if abc!="":
  ans =min(ans,len(abc))

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 + 1 + dpbc[r], res)
                    else:
                        if dpab[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)



        # 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:
stk = {}


def link(x,y): #x>=y前提。合うindを全列挙する

    nm = "".join(x) + "," + "".join(y)
    if nm in stk:
        return stk[nm]

    ret = []
    for i in range(len(x)):

        flag = True
        for j in range(len(y)):

            if j+i >= len(x):
                break

            if x[j+i] != y[j] and x[j+i] != "?" and y[j] != "?":
                flag = False
                break

        if flag:

            ret.append(i)

    for i in range(len(x),2001):
        ret.append(i)

    
    stk[nm] = ret
    
    return ret




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

ans = float("inf")

nl = [[a,b,c],[a,c,b],[b,a,c],[b,c,a],[c,a,b],[c,b,a]]

for x,y,z in nl:

    xy = link(x,y)
    yz = link(y,z)
    xz = link(x,z)

    #print (x,y,z)
    #print (xy)
    #print (xz)
    #print (yz)
    
    dic = {}
    for i in yz:
        dic[i] = 1

    for j in xz:

        for i in xy:

            if i > j:
                break

            if j-i < len(y) and j-i not in yz:
                continue
            else:
                ans = min(ans , max(len(x) , len(y) + i , len(z) + j))
    
                
print (ans)

Submission status: Wrong Answer

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

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(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:

def main():

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

    def LCS(S, T):

        if len(S) == 0 or len(T) == 0:
            return 0
        

        dp = [[0 for _ in range(len(T) + 1)] for _ in range(len(S) + 1)]
        for i in range(len(S)):
            for j in range(len(T)):
                if S[i] == "?" or T[j] == "?" or S[i] == T[j]:
                    dp[i+1][j+1] = dp[i][j] + 1
                else:

                    dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])

        return dp[len(S)][len(T)]

    ans = 0

    ab = LCS(a,b)

    ac = LCS(a,c)

    bc = LCS(b,c)


    if ab == ac == bc == 0:
        print(len(a) + len(b) + len(c))
        exit()

    if ac <= ab and ac <= bc:
        ans = (len(a) + len(b) - ab) + (len(c) - bc) 

    elif ab <= ac and ab <= bc:
        ans = (len(a) + len(c) - ac) + (len(c) - bc) 

    elif bc <= ab and bc <= ac:
        ans = (len(a) + len(b) - ab) + (len(c) - ac)

    print(ans) 


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def align(s1, s2):
    l1 = len(s1)
    l2 = len(s2)
    for i in range(l1):
        f = 0
        for j in range(min(l2, l1-i)):
            if s1[i+j] != s2[j] and s1[i+j] != '?' and s2[j] != '?':
                f = 0
                break
            else:
                f = 1
        if f:
            return s1 + s2[j+1:]
    return s1 + s2

cand = [[a, b, c], [a, c, b], [b, a, c], [b, c, a], [c, a, b], [c, b, a]]

res = 6000

for l in cand:
    tmp = align(l[0], l[1])
    ans = align(tmp, l[2])
    res = min(res, len(ans))

print(res)

Submission status: Wrong Answer

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

def shiritori(p,q):
	len_p = len(p)
	len_q = len(q)

	if len_p <= len_q:
		for i in range(len_q - len_p + 1):
			cond = 1
			for j in range(len_p):
				if p[j] == '?' or q[i+j] == '?':
					continue

				if p[j] != q[i+j]:
					cond = 0
					break
			if cond:
				return q



	cond = 0
	for i in range(len_p+1):
		j = 0
		while True:
			if i == len_p or j == len_q:
				cond = 1
				break
			if p[i] == '?' or q[j] == '?':
				i += 1
				j += 1

			else:
				if p[i] == q[j]:
					i += 1
					j += 1
				else:
					break

		if cond:
			if j == 0:
				return p + q
			else:
				return p[:-j] + q

		else:
			continue

ans = 10**9
ans = min(ans, len(shiritori(shiritori(a,b),c)))
ans = min(ans, len(shiritori(shiritori(a,c),b)))
ans = min(ans, len(shiritori(shiritori(b,a),c)))
ans = min(ans, len(shiritori(shiritori(b,c),a)))
ans = min(ans, len(shiritori(shiritori(c,a),b)))
ans = min(ans, len(shiritori(shiritori(c,b),a)))

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
a = list(input())
b = list(input())
c = list(input())

def equal(s,t):
  # sとtが等しいかどうか
  if len(s)!=len(t):
    return False
  for i in range(len(s)):
    if s[i]!='?' and t[i]!='?' and s[i]!=t[i]:
      return False
  return True

def compare(s,t):
  # sの先頭n文字 と tの末尾n文字が一致している ようなnの最大値
  length = min(len(s), len(t))
  n = 0
  for i in range(1,length+1):
    if equal(s[:i], t[-i:]):
      n = i
  return n

def subset(s,t):
  # sの部分文字列の中にtがあるかどうか
  if len(t)>len(s):
    return False
  for i in range(len(s)-len(t)+1):
    if equal(s[i:i+len(t)], t):
      return True
  return False

def concat(s,t):
  # s->tでくっつける
  c = compare(t,s)
  st = s + t[c:]
  for i in range(c):
    if t[i]!='?':
      st[i+len(s)-c] = t[i]
  return st

def calculator():
  if substring[0] and substring[1]:
    return length[0]
  if substring[2] and substring[3]:
    return length[1]
  if substring[4] and substring[5]:
    return length[2]
  
  result = sum(length)
  
  # a->b
  tmp_str = concat(a,b)
  if subset(tmp_str, c):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(concat(c,tmp_str)), len(concat(tmp_str,c)))
  
  # a->c
  tmp_str = concat(a,c)
  if subset(tmp_str, b):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(concat(b,tmp_str)), len(concat(tmp_str,b)))
  
  # b->a
  tmp_str = concat(b,a)
  if subset(tmp_str, c):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(concat(c,tmp_str)), len(concat(tmp_str,c)))
  
  # b->c
  tmp_str = concat(b,c)
  if subset(tmp_str, a):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(concat(a,tmp_str)), len(concat(tmp_str,a)))
  
  # c->a
  tmp_str = concat(c,a)
  if subset(tmp_str, b):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(concat(b,tmp_str)), len(concat(tmp_str,b)))
  
  # c->b
  tmp_str = concat(c,b)
  if subset(tmp_str, a):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(concat(a,tmp_str)), len(concat(tmp_str,a)))
  
  return result

length = [len(a), len(b), len(c)]
substring = [subset(a,b), subset(a,c), subset(b,a),
             subset(b,c), subset(c,a), subset(c,b)]

print(calculator())

Submission status: Wrong Answer

Solution code:

from collections import deque
from itertools import permutations

def match(s, t):
    # s -> tのマッチ
    res1 = list(s) + ['*'] * len(t)
    res2 = list(t) + ['*'] * len(s)
    
    while True:
        check = True
        for i in range(len(res1)):
            if res1[i] == res2[i]:
                continue

            if res1[i] == '?' or res1[i] == '*':
                continue

            if res2[i] == '?' or res2[i] == '*':
                continue

            check = False
            break

        if check:
            break
        else:
            temp = [res2[-1]]
            temp.extend(res2[:-1])
            res2 = temp


    res = []
    for r1, r2 in zip(res1, res2):
        if r1 == r2 == "*":
            continue

        if r1 == "*":
            res.append(r2)
        elif r1 == "?" and r2 == "*":
            res.append(r1)
        elif r1 == "?" and r2 != "*":
            res.append(r2)
        elif r1 != '?' and r2 == '?':
            res.append(r2)
        else:
            res.append(r1)

    return res


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

    min_len = float('inf')
    res = None
    for s1, s2, s3 in permutations([a, b, c]):
        m = match(s1, s2)
        t = match(m, s3)
        if min_len > len(t):
            min_len = len(t)
            res = t

    print(min_len)



if __name__ == "__main__":
    submit()