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 I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))


"""
aを固定して，b，cの位置を全探索
"""
def main():
    mod=10**9+7
    a=input()
    b=input()
    c=input()
    M=2100

    
    def ch(c1,c2):
        return c1==c2 or c1=="?" or c2=="?"
    
    # aを左端に固定，bがbi文字目から,cが(bからみて)ci文字目から始まるとして，どうなるか
    # 実際にはabcの並びが6通り
    def calc(s1,s2,s3):
        
        # 遠く離れている場合などもokなので，初期値は1，ダメなら0に
        ab=[1]*(2*M)
        ac=[1]*(2*M)
        bc=[1]*(2*M)
        
        Na=len(s1)
        Nb=len(s2)
        Nc=len(s3)
        
        
        for bi in range(Na+5):#aの何文字目から?
            for j in range(Nb):#bの何文字目？
                if bi+j>=Na:
                    break
                if not ch(s1[bi+j], s2[j]):
                    ab[bi]=0
                    break
                
        for ci in range(Na+5):#aの何文字目から?
            for j in range(Nc):#cの何文字目？
                if ci+j>=Na:
                    break
                if not ch(s1[ci+j], s3[j]):
                    ac[ci]=0
                    break
                
        for ci in range(Nb+5):#bの何文字目から?
            for j in range(Nc):#cの何文字目？
                if ci+j>=Nb:
                    break
                if not ch(s2[ci+j], s3[j]):
                    bc[ci]=0
                    break
                
        ans=10**5
        for i in range(Na+5):#bの開始位置
            for j in range(i,Na+Nb+5):#cの開始位置
                if ab[i] and ac[j] and bc[j-i]:
                    ans=min(ans,j+Nc)
                    
        # print(ab)
        # print(ac)
        # print(bc)
                    
        return ans
    
    ans=len(a)+len(b)+len(c)
    
    ans=min(ans,calc(a,b,c))
    ans=min(ans,calc(a,c,b))
    ans=min(ans,calc(b,a,c))
    ans=min(ans,calc(b,c,a))
    ans=min(ans,calc(c,a,b))
    ans=min(ans,calc(c,b,a))
    
    print(ans)
                
            

main()

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


"""
aを固定して，b，cの位置を全探索
"""
def main():
    mod=10**9+7
    a=input()
    b=input()
    c=input()
    M=2100

    
    def ch(c1,c2):
        return c1==c2 or c1=="?" or c2=="?"
    
    # aを左端に固定，bがbi文字目から,cが(bからみて)ci文字目から始まるとして，どうなるか
    # 実際にはabcの並びが6通り
    def calc(s1,s2,s3):
        
        # 遠く離れている場合などもokなので，初期値は1，ダメなら0に
        ab=[1]*(2*M)
        ac=[1]*(2*M)
        bc=[1]*(2*M)
        
        Na=len(s1)
        Nb=len(s2)
        Nc=len(s3)
        
        
        for bi in range(Na):#aの何文字目から?
            for j in range(Nb):#bの何文字目？
                if bi+j>=Na:
                    break
                if not ch(s1[bi+j], s2[j]):
                    ab[bi]=0
                    break
                
        for ci in range(Na):#aの何文字目から?
            for j in range(Nc):#cの何文字目？
                if ci+j>=Na:
                    break
                if not ch(s1[ci+j], s3[j]):
                    ac[ci]=0
                    break
                
        for ci in range(Na):#bの何文字目から?
            for j in range(Nc):#cの何文字目？
                if ci+j>=Nb:
                    break
                if not ch(s2[ci+j], s3[j]):
                    bc[ci]=0
                    break
                
        ans=10**5
        for i in range(Na+5):#bの開始位置
            for j in range(i,Na+Nb+5):#cの開始位置
                if ab[i] and ac[j] and bc[j-i]:
                    ans=min(ans,j+Nc)
                    
        # print(ab)
        # print(ac)
        # print(bc)
                    
        return ans
    
    ans=10**5
    
    ans=min(ans,calc(a,b,c))
    ans=min(ans,calc(a,c,b))
    ans=min(ans,calc(b,a,c))
    ans=min(ans,calc(b,c,a))
    ans=min(ans,calc(c,a,b))
    ans=min(ans,calc(c,b,a))
    
    print(ans)
                
            
        
            
 

main()

Submission status: Wrong Answer

Solution code:
from itertools import permutations
ans = 6000
for a, b, c in permutations(input() for _ in range(3)):
    ab, bc, ac = [], [], []
    for s, t, st in [(a, b, ab), (b, c, bc), (a, c, ac)]:
        for i in range(len(s) + 1):
            for si, ti in zip(s[i:], t):
                if si != '?' and ti != '?' and si != ti:
                    st.append(False)
                    break
            else:
                st.append(True)
    for i in range(len(a)+1):
        for j in range(len(b)+1):
            if ab[i] and bc[j] and (len(a) < i+j or ac[i+j]):
                ans = min(ans, max(len(a), i+len(b), i+j+len(c)))
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import permutations
ans = 6000
for a, b, c in permutations(input() for _ in range(3)):
    ab, bc, ac = [], [], []
    for s, t, st in [(a, b, ab), (b, c, bc), (a, c, ac)]:
        for i in range(len(s) + 1):
            for si, ti in zip(s[i:], t):
                if si != '?' and ti != '?' and si != ti:
                    st.append(False)
                    break
            else:
                st.append(True)
    for i in range(len(a)+1):
        for j in range(len(b)+1):
            if ab[i] and bc[j] and (len(a) < i+j or ac[i+j]):
                ans = min(ans, i+j+len(c))
print(ans)

Submission status: Wrong Answer

Solution code:
MAX_N = 2000
a = [ord(char) for char in input()]
b = [ord(char) for char in input()]
c = [ord(char) for char in input()]


len_a = len(a)
len_b = len(b)
len_c = len(c)

a = a + [63] * (MAX_N - len_a + 10)
b = b + [63] * (MAX_N - len_b + 10)
c = c + [63] * (MAX_N - len_c + 10)

ab = [True] * (2 * MAX_N + 10)
ba = [True] * (2 * MAX_N + 10)
bc = [True] * (2 * MAX_N + 10)
cb = [True] * (2 * MAX_N + 10)
ca = [True] * (2 * MAX_N + 10)
ac = [True] * (2 * MAX_N + 10)


for length in range(MAX_N + 1):
    for i in range(MAX_N + 1):
        if i + length > MAX_N:
            break

        if a[i + length] != b[i] and a[i + length] != 63 and b[i] != 63:
            ab[length] = False
        if b[i + length] != a[i] and b[i + length] != 63 and a[i] != 63:
            ba[length] = False

        if b[i + length] != c[i] and b[i + length] != 63 and c[i] != 63:
            bc[length] = False
        if c[i + length] != b[i] and c[i + length] != 63 and b[i] != 63:
            cb[length] = False

        if c[i + length] != a[i] and c[i + length] != 63 and a[i] != 63:
            ca[length] = False
        if a[i + length] != c[i] and a[i + length] != 63 and c[i] != 63:
            ac[length] = False


ans = MAX_N * 6
"""
for len1 in range(MAX_N + 1):
    for len2 in range(MAX_N + 1):
        if ab[len1] and bc[len2] and ac[len1 + len2]:
            tmp = max(len_a, len_b + len1, len_c + len1 + len2)
            ans = min(ans, tmp)
        if ba[len1] and ac[len2] and bc[len1 + len2]:
            tmp = max(len_b, len_a + len1, len_c + len1 + len2)
            ans = min(ans, tmp)

for len1 in range(MAX_N + 1):
    for len2 in range(MAX_N + 1):
        if bc[len1] and ca[len2] and ba[len1 + len2]:
            tmp = max(len_b, len_c + len1, len_a + len1 + len2)
            ans = min(ans, tmp)
        if cb[len1] and ba[len2] and ca[len1 + len2]:
            tmp = max(len_c, len_b + len1, len_a + len1 + len2)
            ans = min(ans, tmp)

for len1 in range(MAX_N + 1):
    for len2 in range(MAX_N + 1):
        if ca[len1] and ab[len2] and cb[len1 + len2]:
            tmp = max(len_c, len_a + len1, len_b + len1 + len2)
            ans = min(ans, tmp)
        if ac[len1] and cb[len2] and ab[len1 + len2]:
            tmp = max(len_a, len_c + len1, len_b + len1 + len2)
            ans = min(ans, tmp)
"""
for len1 in range(MAX_N + 1):
    for len2 in range(MAX_N + 1):
        if ab[len1] and bc[len2] and ac[len1 + len2]:
            pass
        if ba[len1] and ac[len2] and bc[len1 + len2]:
            pass

        if bc[len1] and ca[len2] and ba[len1 + len2]:
            pass
        if cb[len1] and ba[len2] and ca[len1 + len2]:
            pass

        if ca[len1] and ab[len2] and cb[len1 + len2]:
            pass
        if ac[len1] and cb[len2] and ab[len1 + len2]:
            pass

print(ans)

Submission status: Wrong Answer

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


len_a = len(a)
len_b = len(b)
len_c = len(c)

a = a + "?" * (2 * MAX_N)
b = b + "?" * (2 * MAX_N)
c = c + "?" * (2 * MAX_N)

ab = [True] * (2 * MAX_N + 10)
ba = [True] * (2 * MAX_N + 10)
bc = [True] * (2 * MAX_N + 10)
cb = [True] * (2 * MAX_N + 10)
ca = [True] * (2 * MAX_N + 10)
ac = [True] * (2 * MAX_N + 10)


for length in range(MAX_N + 1):
    for i in range(MAX_N + 1):
        if a[i + length] != b[i] and a[i + length] != "?" and b[i] != "?":
            ab[length] = False
        if b[i + length] != a[i] and b[i + length] != "?" and a[i] != "?":
            ba[length] = False

        if b[i + length] != c[i] and b[i + length] != "?" and c[i] != "?":
            bc[length] = False
        if c[i + length] != b[i] and c[i + length] != "?" and b[i] != "?":
            cb[length] = False

        if c[i + length] != a[i] and c[i + length] != "?" and a[i] != "?":
            ca[length] = False
        if a[i + length] != c[i] and a[i + length] != "?" and c[i] != "?":
            ac[length] = False

ans = MAX_N * 6
for len1 in range(MAX_N + 1):
    for len2 in range(MAX_N + 1):
        if ab[len1] and bc[len2] and ac[len1 + len2]:
            ans = min(ans, len1 + len2 + len_c)
        if ba[len1] and ac[len2] and bc[len1 + len2]:
            ans = min(ans, len1 + len2 + len_c)

        if bc[len1] and ca[len2] and ba[len1 + len2]:
            ans = min(ans, len1 + len2 + len_a)
        if cb[len1] and ba[len2] and ca[len1 + len2]:
            ans = min(ans, len1 + len2 + len_a)

        if ca[len1] and ab[len2] and cb[len1 + len2]:
            ans = min(ans, len1 + len2 + len_b)

        if ac[len1] and cb[len2] and ab[len1 + len2]:
            ans = min(ans, len1 + len2 + len_b)

print(ans)

Submission status: Wrong Answer

Solution code:

Submission status: Wrong Answer

Solution code:
# パナソニック2020E

import sys
input = sys.stdin.readline
sys.setrecursionlimit(max(1000, 10**9))


a,b,c = [input() for _ in range(3)]
la, lb, lc = len(a), len(b), len(c)

def ok(c1,c2):
    return True if c1==c2 or c1=="?" or c2=="?" else False
def sub(s,t,gap=0):
    # tを固定したとき、
    # sの開始位置座標としてありえるもののset
    S = set(list(range(-len(s)-gap, -len(s)+1)) + list(range(len(t), len(t)+gap+1)))
    i1 = min(len(s), len(t))
    i2 = max(len(s), len(t))
    i3 = len(s)+len(t)
    for i in range(1,i1+1):
        if all((ok(s[-i+ii], t[ii])) for ii in range(i)):
            S.add(i-len(s))
    for i in range(i1, i2):
        if len(s)<len(t) and all((ok(s[ii], t[ii+i-i1]) for ii in range(i1))):
            S.add(i-len(s))
        elif len(t)<len(s) and all(ok(s[i2-i+ii], t[ii]) for ii in range(i1)):
            S.add(i-len(s))
    for i in range(i2, i3):
        if all((ok(s[ii], t[i-i3+ii]) for ii in range(i3-i))):
            S.add(i-len(s))
    return S

ab = sub(a,b, lc)
bc = sub(c,b, la)
ac = sub(a,c, lb)
ans = 10**9
import itertools
for ii, jj in itertools.product(ab, bc):
    if ii - jj in ac:
        val = max(lb, ii+la, jj+lc) - min(0, ii, jj)
        ans = min(ans, val)
print(ans)

Submission status: Wrong Answer

Solution code:
# パナソニック2020E

import sys
input = sys.stdin.readline
sys.setrecursionlimit(max(1000, 10**9))


a,b,c = [input() for _ in range(3)]
la, lb, lc = len(a), len(b), len(c)

def ok(c1,c2):
    return True if c1==c2 or c1=="?" or c2=="?" else False
def sub(s,t,gap):
    # tを固定したとき、
    # sの開始位置座標としてありえるもののset
    S = set(list(range(-len(s)-gap, -len(s)+1)) + list(range(len(t), len(t)+gap)))
    i1 = min(len(s), len(t))
    i2 = max(len(s), len(t))
    i3 = len(s)+len(t)
    for i in range(1,i1):
        if all((ok(s[-i+ii], t[ii])) for ii in range(i)):
            S.add(i-len(s))
    for i in range(i1, i2):
        if len(s)<len(t) and all((ok(s[ii], t[ii+i-i1]) for ii in range(i1))):
            S.add(i-len(s))
        elif len(t)<=len(s) and all(ok(s[i1-i+ii], t[ii]) for ii in range(i1)):
            S.add(i-len(s))
    for i in range(i2, i3):
        if all((ok(s[ii], t[i-i3+ii]) for ii in range(i3-i))):
            S.add(i-len(s))
    return S

ab = sub(a,b, lc)
bc = sub(c,b, la)
ac = sub(a,c, lb)
ans = 10**9
import itertools
for ii, jj in itertools.product(ab, bc):
    if ii - jj in ac:
        val = max(lb, ii+la, jj+lc) - min(0, ii, jj)
        ans = min(ans, val)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import fractions
#import sys
#input = sys.stdin.readline

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ZipArray(a):
    aa = [[a[i],i]for i in range(n)]

    aa.sort(key = lambda x : x[0])
    for i in range(n):
        aa[i][0]=i+1
    aa.sort(key = lambda x : x[1])
    b=[aa[i][0] for i in range(len(a))]
    return b

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans

def Zeros(a,b):
    if(b<=-1):
        return [0 for i in range(a)]
    else:
        return [[0 for i in range(b)] for i in range(a)]

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 2
N = 10 ** 6 + 2
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

'''

def rln(s):
    return range(len(s))

#a = list(map(int, input().split()))

#################################################
#################################################
#################################################
#################################################


s1 = input()
s2 = input()
s3 = input()

def CalcMin(a,b,c):
    an = len(a)
    bn = len(b)
    cn = len(c)
    
    ans = 10**9
    anspos = 10**9
    ab = ""
    for bpos in range(-2000,2000,1):
        minl = min(0,bpos)
        minr = min(an, bpos+bn)
        maxl = max(0,bpos)
        maxr = max(an, bpos+bn)
        ok = 1
        for j in range(minr - maxl):
            if(a[maxl+j] != b[maxl-bpos+j] and a[maxl+j]!='?' and b[maxl-bpos+j]!='?'):
                ok = 0
        if(ok==1 and ans>maxr-minl):
            ans = maxr-minl
            anspos = bpos
    ab = ""
    start = min(0,anspos)
    for i in range(start,start+ans):
        letter = '?'
        if(i>=0 and i<an):
            if(a[i]!='?'):
                letter = a[i]
        if(i>=anspos and i<anspos+bn):
            #print(anspos,i)
            if(b[i-anspos]!='?'):
                letter = b[i-anspos]
        ab+=letter
    #print(ab)
    
    abn = len(ab)
    ans = 10**9
    anspos = 10**9
    for cpos in range(-4000,4000,1):
        minl = min(0,cpos)
        minr = min(abn, cpos+cn)
        maxl = max(0,cpos)
        maxr = max(abn, cpos+cn)
        ok = 1
        for j in range(minr - maxl):
            if(ab[maxl+j] != c[maxl-cpos+j] and ab[maxl+j]!='?' and c[maxl-cpos+j]!='?'):
                ok = 0
        if(ok==1 and ans>maxr-minl):
            ans = maxr-minl
            anspos = cpos
    
    return ans

anss = [CalcMin(s1,s2,s3),CalcMin(s1,s3,s2),CalcMin(s2,s1,s3),CalcMin(s2,s3,s1),CalcMin(s3,s1,s2),CalcMin(s3,s2,s1)]

#print(anss)
print(min(anss))

Submission status: Wrong Answer

Solution code:
words = [raw_input() for _ in range(3)]
m = [sum(map(len,words))]

def g(a,b):
	if len(a) != len(b):
		return False
	
	for la,lb in zip(a,b):
		if not((la == lb) or (la == '?' or lb == '?')):
			return False
	return True

def f(cur, word):
	if len(cur) == 0:
		return cur + list(word)

	l = list(word)
	for length in range(len(l))[::-1]:
		if g(cur[-length:],l[:length]):
			return cur[:-length] + l
	return cur + list(word)
def dfs(i, cur, words, m):
	if i==3:
		m[0] = min(m[0], len(cur))
		return

	dfs(i+1, f(cur, words[i]), words, m)

def permutations(i, words):
	if i == len(words):
		
		dfs(0,[],words[::], m)
		return 
	for j in range(i, len(words)):
		words[i], words[j] = words[j] , words[i]
		permutations(i+1, words)
		words[i], words[j] = words[j] , words[i]
permutations(0, words)
print m[0]

Submission status: Wrong Answer

Solution code:
words = [raw_input() for _ in range(3)]
m = [sum(map(len,words))]

def g(a,b):
	if len(a) != len(b):
		return False

	for la,lb in zip(a,b):
		if not(la == lb or (la == '?' or lb == '?')):
			return False
	return True

def f(cur, word):
	if len(cur) == 0:
		return cur + list(word)

	l = list(word)
	for length in range(len(l))[::-1]:
		if g(cur[-length:],l):
			return cur[:-length] + l
	return cur + list(word)
def dfs(i, cur, words, m):
	if i==3:

		m[0] = min(m[0], len(cur))
		return

	dfs(i+1, f(cur, words[i]), words, m)

def permutations(i, words):
	if i == len(words):
		dfs(0,[],words[::], m)
		return 
	for j in range(i, len(words)):
		words[i], words[j] = words[j] , words[i]
		permutations(i+1, words)
		words[i], words[j] = words[j] , words[i]
permutations(0, words)
print m[0]

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline

A = list(map(lambda x:ord(x)-63, readline().strip()))
B = list(map(lambda x:ord(x)-63, readline().strip()))
C = list(map(lambda x:ord(x)-63, readline().strip()))

LA = len(A)
LB = len(B)
LC = len(C)
geta = 4002

tAB = [True]*(geta + 4)
for i in range(-geta//2, geta//2+1):
    for j in range(LB):
        if 0 <= j+i < LA and A[i+j] and B[j] and A[i+j] != B[j]:
            tAB[i] = False
            break

tAC = [True]*(geta + 4)
for i in range(-geta//2, geta//2+1):
    for j in range(LC):
        if 0 <= j+i < LA and A[i+j] and C[j] and A[i+j] != C[j]:
            tAC[i] = False
            break


tBC = [True]*(2*geta + 4)
for i in range(-geta, geta+1):
    for j in range(LC):
        if 0 <= j+i < LB and B[i+j] and C[j] and B[i+j] != C[j]:
            tBC[i] = False
            break

ans = 3*geta
for ib in range(-geta//2, geta//2+1):
    if tAB[ib]:
        for ic in range(-geta//2, geta//2+1):
            if tAC[ic]:
                if -geta <= ic-ib <= geta and not tBC[ic-ib]:
                    continue
                l = min(0, ib, ic)
                r = max(LA-1, ib+LB-1, ic+LC-1)
                ans = min(ans, r-l+1)
print(ans)

Submission status: Wrong Answer

Solution code:
a="a?c"
b="der"
c="cod"
A=len(a)
B=len(b)
C=len(c)
d={a:A, b:B, c:C}
d=sorted(d.items(),key=lambda x:x[1])
a=d[2][0]
b=d[1][0]
c=d[0][0]
A=d[2][1]
B=d[1][1]
C=d[0][1]
ans=[[a+b,A+B],[b+a,A+B]]
ans1=[[a+c,A+C],[c+a,C+A]]

def find(a,b,A,B,ans):
  for i in range(1,A+B):
    if i<=B:
      num=0
      for j in range(i):
        if a[j]==b[B-i+j] or (a[j]=="?") or (b[B-i+j]=="?"):
          num+=1
        else:
          break
        if num==i:
          ans.append([b[:B-i]+a,B-i+A])
          print("!")
    elif i<A:
      num=0
      for j in range(B):
        if a[i-B+1+j]==b[j] or (a[i-B+1+j]=="?") or (b[j]=="?"):
          num+=1
        else:
          break
        if num==i:
          ans.append([a,A])
          print("!!")
    else:
      num=0
      for j in range(A+B-i):
        if (a[i-B+j]==b[j]) or (a[i-B+j]=="?") or (b[j]=="?"):
          num+=1
        else:
          break
        if num==A+B-i:
          ans.append([a+b[A-i:],i])
          print("ans+",[a+b[A-i:],i])
  return ans

ans=find(a,b,A,B,ans)
an=[]
for i in ans:
  an=find(i[0],c,i[1],C,an)
ans1=find(a,c,A,C,ans1)
#print(an)
for i in ans1:
  an=find(i[0],b,i[1],B,an)
mi=A+B+C
for i in an:
  if i[1]<mi:
    mi=i[1]
print(mi)

Submission status: Wrong Answer

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

def cf(a,b):
    if a==b or a=='?' or b=='?': return True
    return False

def check(s1,s2):
    l1, l2=len(s1), len(s2)

    count1=0
    p1=0
    p2=0

    while p1<l1 and p2<l2 and p2+p1<l1:
        if cf(s1[p1+p2],s2[p2]):
            count1+=1
            p2+=1

        else:
            p1+=1
            p2=0
            count1=0
    return count1

def cn(s1,s2,s3):
    l1, l2, l3 =len(s1), len(s2), len(s3)
    count1=check(s1,s2)
    count2=check(s2,s3)



    ans=l1+l2+l3-count1-count2
    #print(count1,count2,ans)
    return ans

c1=cn(a,b,c)
c2=cn(a,c,b)
c3=cn(b,a,c)
c4=cn(b,c,a)
c5=cn(c,a,b)
c6=cn(c,b,a)

ans=min(c1,c2,c3,c4,c5,c6)
print(ans)

Submission status: Wrong Answer

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


def cn(s1,s2,s3):
    l1, l2, l3 =len(s1), len(s2), len(s3)

    count1=0
    for i in range(0,min(l1,l2)):
        if s1[l1-1-i]==s2[i] or s1[0]=='?' or s2[0]=='?':
            count1+=1
        else:break

    count2=0
    for i in range(0,min(l2,l3)):
        if s2[l2-1-i]==s3[i] or s2[0]=='?' or s3[0]=='?':
            count2+=1
        else: break

    ans=l1+l2+l3-count1-count2
    return ans

c1=cn(a,b,c)
c2=cn(a,c,b)
c3=cn(b,a,c)
c4=cn(b,c,a)
c5=cn(c,a,b)
c6=cn(c,b,a)

ans=min(c1,c2,c3,c4,c5,c6)
print(ans)

Submission status: Wrong Answer

Solution code:
a = list(input())
b = list(input())
c = list(input())
la = len(a)
lb = len(b)
lc = len(c)
o = ((a,b,c), (a,c,b), (b,a,c), (b,c,a), (c,a,b), (c,b,a))
l = ((la,lb,lc), (la,lc,lb), (lb,la,lc), (lb,lc,la), (lc,la,lb), (lc,lb,la))

ans = [0] * 6

for i in range(6):
    d = o[i][0].copy()
    e = o[i][1].copy()
    f = o[i][2].copy()
    ld = l[i][0]
    le = l[i][1]
    lf = l[i][2]

    #1
    tf = False

    #1-1
    if ld <= le:
        for j in range(ld):
            if tf == True:
                break

            res = []
            for k in range(ld-j):
                if d[j+k] != e[k] and e[k] != '?' and d[j+k] != '?':
                    break

                if d[j+k] == '?' and e[k] != '?':
                    res.append(k)

                if k == ld - j - 1:
                    d += e[ld - j:]
                    for m in res:
                        d[j+m] = e[m]
                    tf = True
                    ld = le + j

        if tf == False:
            d += e
            ld = ld + le

    #1-2
    else:
        for j in range(ld):
            if tf == True:
                break

            res = []

            if j <= ld - le:
                for k in range(le):
                    if d[j+k] != e[k] and e[k] != '?' and d[j+k] != '?':
                        break

                    if d[j+k] == '?' and e[k] != '?':
                        res.append(k)

                    if k == ld - j - 1:
                        for m in res:
                            d[j+m] = e[m]
                        tf = True
            
            else:
                for k in range(ld-j):
                    if d[j+k] != e[k] and e[k] != '?' and d[j+k] != '?':
                        break

                    if d[j+k] == '?' and e[k] != '?':
                        res.append(k)

                    if k == ld - j - 1:
                        d += e[ld - j:]
                        for m in res:
                            d[j+m] = e[m]
                        tf = True
                        ld = le + j

        if tf == False:
            d += e
            ld = ld + le


    #2
    tf = False

    #2-1
    if ld <= lf:
        for j in range(ld):
            if tf == True:
                break

            res = []
            for k in range(ld-j):
                if d[j+k] != f[k] and f[k] != '?' and d[j+k] != '?':
                    break

                if d[j+k] == '?' and f[k] != '?':
                    res.append(k)

                if k == ld - j - 1:
                    d += f[ld - j:]
                    for m in res:
                        d[j+m] = f[m]
                    tf = True
                    ans[i] = lf + j

        if tf == False:
            d += f
            ans[i] = ld + lf

    #2-2
    else:
        for j in range(ld):
            if tf == True:
                break

            res = []

            if j <= ld - lf:
                for k in range(lf):
                    if d[j+k] != f[k] and f[k] != '?' and d[j+k] != '?':
                        break

                    if d[j+k] == '?' and f[k] != '?':
                        res.append(k)

                    if k == ld - j - 1:
                        for m in res:
                            d[j+m] = f[m]
                        tf = True
                        ans[i] = ld
            
            else:
                for k in range(ld-j):
                    if d[j+k] != f[k] and f[k] != '?' and d[j+k] != '?':
                        break

                    if d[j+k] == '?' and f[k] != '?':
                        res.append(k)

                    if k == ld - j - 1:
                        d += f[ld - j:]
                        for m in res:
                            d[j+m] = f[m]
                        tf = True
                        ans[i] = lf + j

        if tf == False:
            d += f
            ans[i] = ld + lf

print(min(ans))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
A = input()
B = input()
C = input()

def _check(st1, st2):
    if len(st1) != len(st2):
        return False
    if st1 == st2:
        return True
    for i in range(len(st1)):
        if st1[i] == st2[i]:continue
        if (st1[i]=='?')|(st2[i]=='?'):continue
        else:
            return False
    return True

def check(ABC,x,y):
    A,B,C = ABC
    if _check(A[x:x+len(B)], B[:len(A)-x]):
        if _check(B[y:y+len(C)], C[:len(B)-y]):
            if _check(A[x+y:], C[:len(A)-x-y]):
                return max([len(A), len(B)+x, len(C)+x+y])
    return len(A)+len(B)+len(C)


if __name__ == '__main__':
    min_val = 10**10
    patterns = [[A,B,C], [A,C,B], [B,A,C], [B,C,A], [C,A,B], [C,B,A]]
    for pattern in patterns:
        max_x = len(pattern[0])
        max_y = len(pattern[1])
        for x in range(max_x+1):
            for y in range(max_y+1):
                val = check(pattern, x,y)
                if min_val > val:
                    min_val = val
    print(min_val)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
A = input()
B = input()
C = input()

def shift(pre, merge, sur):
    if (pre =='') | (sur==''):
        return (False, '', pre+merge+sur, '')
    if pre[-1] == sur[0]:
        merged = pre[-1] + merge
    elif (pre[-1] == '?') | (sur[0] == '?'):
        if pre[-1] == '?':
            merged = sur[0] + merge
        else:
            merged = pre[-1] + merge
    else:
        return (False, pre, merge, sur)
    return (True, pre[:-1], merged, sur[1:])

def search(A,B,C):
    pre = A
    sur = B
    merged = ''
    min_val = 10**10
    while True:
        is_next, pre, merged, sur = shift(pre, merged, sur)

        pre2 = pre+merged+sur
        sur2 = C
        merged2 = ''
        while True:
            is_next2, pre2, merged2, sur2 = shift(pre2, merged2, sur2)
            if is_next2:
                continue
            else:break
        val = len(pre2+merged2+sur2)
        if min_val > val:
            min_val = val
        if not is_next:
            break
    return min_val



if __name__ == '__main__':
    vals = []
    patterns = [[A,B,C], [A,C,B], [B,A,C], [B,C,A], [C,A,B], [C,B,A]]
    for pattern in patterns:
        vals.append(search(*pattern))
    print(min(vals))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


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

def check(x, y):
    # xの後にy
    x_len = len(x)
    y_len = len(y)
    L = [0] * (x_len + 1)
    for y_start in range(x_len + 1):
        for j in range(y_len):
            if y_start + j < x_len and x[y_start + j] != y[j] and x[y_start + j] != '?' and y[j] != '?':
                break
        else:
            L[y_start] = 1
    return L


ans = total_len
for i, j, k in permutations((a, b, c)):
    ij_list = check(i, j)
    jk_list = check(j, k)
    ik_list = check(i, k)
    i_len = len(i)
    j_len = len(j)
    k_len = len(k)
    for ij_gap in range(i_len + 1):
        if ij_list[ij_gap] == 0:
            continue
        for jk_gap in range(j_len + 1):
            if ij_list[ij_gap] == 0 or jk_list[jk_gap] == 0:
                continue
            if ij_gap + jk_gap < i_len and ik_list[ij_gap + jk_gap] == 0:
                continue
            ans = min(ans, max(ij_gap + jk_gap + k_len, ij_gap + j_len, i_len))

print(ans)