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:
#!/usr/bin/env python3
import sys

def getNextMin(a,b,bi,c):
    l = max(len(a)+1,bi+len(b)+1)
    for i in range(bi,l):
        match = True
        for j in range(len(c)):
            if c[j] == '?':
                continue
            if i+j < len(a) and a[i+j] != '?' and a[i+j] != c[j]:
                match = False
                break
            if i-bi+j < len(b) and b[i-bi+j] != '?' and b[i-bi+j] != c[j]:
                match = False
                break
        if match:
            return max(len(a),len(b)+bi,len(c)+i)
        # if match:
        #     substr = ""
        #     for k in range(bi):
        #         substr+=" "
        #     substr2 = ""
        #     for j in range(i):
        #         substr2+=" "
        #     r = max(len(a),len(b)+bi,len(c)+i)
        #     print(f"___MATCH___{bi}-{i}---->{r}")
        #     print(''.join(a))
        #     print(substr + ''.join(b))
        #     print(substr2 + ''.join(c))
        #     break
    

def getMin(a,b,c):
    for i in range(len(a)+1):
        match = True
        bi = i
        for j in range(len(b)):
            if i+j >=len(a):
                break
            if a[i+j] != '?' and b[j] != '?' and a[i+j] != b[j]:
                match = False
                break
        if match:
            return getNextMin(a,b,bi,c)
            


def solve(a: str, b: str, c: str):
    a = list(a)
    b = list(b)
    c = list(c)
    print(min(getMin(a,b,c),getMin(a,c,b),getMin(b,c,a),getMin(b,a,c),getMin(c,a,b),getMin(c,b,a)))
    return


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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

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)

    print(abc-max(p1,p2,p3,p4,p5,p6))

    return

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

Submission status: Wrong Answer

Solution code:
import bisect
import math

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))


INF = 1 << 29

DIV = (10**9) + 7


def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def ncr(n, r, mod=DIV):
	r = min(r, n-r)
	ret = 1
	for i in range(1, r+1):
		ret = ret * (n-i+1) % mod
		ret = ret * mod_inv(i, mod) % mod
	return ret


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % mod) % mod
    return CONV()


def matchAB(a,b):
    #print ("matching",a,b)
    tmp = -1
    tmp_res =""
    for ai_start in range(len(a)):
        ai = ai_start
        notMached= False
        for bi,bc in enumerate(b):
            ac = a[ai]
            if ac!="?" and bc!="?" and ac!=bc:
                notMached = True
                break
            ai+=1
            if ai == len(a):
                if bi!=len(b):
                    tmp_res = b[bi+1:]
                break
        
        if notMached == False:
            tmp = ai_start
            if ai != len(a):
                tmp_res = a[ai+1:]  
            break
    if tmp == -1:
        #print(a+b, tmp, tmp_res)
        return a+b
    else:
        ans = a[:ai_start]
        for aidx in range(ai_start, ai_start+ min(len(a)-ai_start,len(b))):
            if a[aidx]=="?":
                ans += b[aidx-ai_start]
            else:
                ans+=a[aidx]
        #print (ans,tmp,tmp_res)
        return ans+tmp_res

def matchABC(a,b,c):
    s = matchAB(matchAB(a, b), c)
    #print(s)
    return len(s)

def main():
    a = sRaw()
    b = sRaw()
    c = sRaw()
    #cand1 = len(matchAB(matchAB(a,b),c))
    #cand2 = len(matchAB(matchAB(a,c), b))
    #cand3 = len(matchAB(matchAB(b,a), c))
    #cand4 = len(matchAB(matchAB(b,c), a))
    #cand5 = len(matchAB(matchAB(c, a), b))
    #cand6 = len(matchAB(matchAB(c, b), a))
    cand1 = matchABC(a,b,c)
    cand2 = matchABC(a,c,b)
    cand3 = matchABC(b,a,c)
    cand4 = matchABC(b,c,a)
    cand5 = matchABC(c,a,b)
    cand6 = matchABC(c,b,a)#"""
    return min(cand1,cand2,cand3,cand4,cand5,cand6)
    
if __name__ == "__main__":
    print(main())

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] = '?'
  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:
a=input()
b=input()
c=input()
n=len(a)+len(b)+len(c)
x=[]
def comp(a,b):
    res=True
    for i in range(len(a)):
        if a[i]!=b[i] and a[i]!='?' and b[i]!='?':
            res=False
    return (res)
def comp2(a,b):
    if len(a)<len(b):
        i=len(a)
    else:
        i=len(b)
    while (not comp(a[len(a)-i:],b[0:i])) and i>0:
        i-=1
    return (i)
ab=comp2(a,b)
ac=comp2(a,c)
ba=comp2(b,a)
bc=comp2(b,c)
ca=comp2(c,a)
cb=comp2(c,b)
x.append (ab+bc)
x.append (ac+cb)
x.append (ba+ac)
x.append (bc+ca)
x.append (ca+ab)
x.append (cb+ba)

print(n-max(x))

Submission status: Wrong Answer

Solution code:
class KMP:
    def __init__(self, P):
        self.P = P
        self.N = len(P)
        self.T = [0] * (self.N + 1)
        self._compile()

    def _compile(self):
        j = 0
        self.T[0] = -1
        for i in range(1, self.N):
            self.T[i] = j
            if self.P[i] == self.P[j]:
                j+=1
            elif self.P[i]=='?' or self.P[j]=='?':
                j+=1
            else:
                j=0
        self.T[self.N] = j
        #print(self.T)
      

    def search(self, S):
        NS = len(S)
        i = m = 0
        A = []
        while m + i < NS:
            if self.P[i] == S[m + i] or self.P[i]=='?' or S[m+i]=='?':#
                i += 1
                if i != self.N:
                    continue
                A.append(m)
            m += i - self.T[i]
            i = max(0, self.T[i])
        return A

a=list(input())
b=list(input())
c=list(input())
s=[a,b,c]
zn=len(a)+len(b)+len(c)
def solve(a,b,c):
    z=a+['?']*zn
    r=len(a)
    kl=KMP(b).search(z)
    k=min(kl)
    for i in range(len(b)):
        if b[i]!='?':
            z[k+i]=b[i]
    r=max(r,k+len(b))
    kl=KMP(c).search(z)
    k=min(kl)
    #print(kl)
    #print(''.join(z))
    return max(r,k+len(c))
ans=10**10
for i in range(3):
    for j in range(3):
        for k in range(3):
            if i==j or j==k or k==i:
                continue
            ans=min(ans,solve(s[i],s[j],s[k]))
print(ans)

Submission status: Wrong Answer

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


def check(x, y):
    for i, j in zip(x, y):
        if i == "?" or j == "?":
            continue
        if i == j:
            continue
        return False
    return True


# x, yの順で結合する
def unite(x, y):
    cnt = min(len(x), len(y))
    while True:
        r = x[-cnt:]
        s = y[:cnt]
        if check(r, s):
            return x + y[cnt:]
        else:
            cnt -= 1
        if cnt == 0:
            break
    return x + y


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

Submission status: Wrong Answer

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


def check(S, T):
    """
    S,Tの順に結合する
    """
    Ns = len(S)
    Nt = len(T)
    match = 0
    for i, s in enumerate(S):
        if s == T[0] or s == "?":
            x = i
            y = 0
            while x < Ns and y < Nt:
                if S[x] == "?" or T[y] == "?" or S[x] == T[y]:
                    x += 1
                    y += 1
                    continue
                else:
                    break
            else:
                match = max(match, y)
    suffix = T[match:]
    return S + suffix


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

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

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(p + r4)
if r4 == 0:
    res.append(p + r3)
if r5 == 0:
    res.append(p + r6)
if r6 == 0:
    res.append(p + r5)
print(min(res))

Submission status: Wrong Answer

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

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

def concat(x,y):
    len_x = len(x)
    len_y = len(y)
    for i in range(len_x+1):
        z = x[:i]
        ng = False
        for j in range(len_y):
            # log(x,y,i,j)
            if i+j >= len_x:
                z += y[j]
            elif x[i+j]=="?" or y[j]=="?" or x[i+j]==y[j]:
                if x[i+j]=="?" and y[j]=="?":
                    z += "?"
                else:
                    z += x[i+j] if x[i+j] != "?" else y[j]
            else:
                ng = True
                break
        if not ng:
            log(x,y,z+x[i+j+1:])
            return z+x[i+j+1:]
    log("XXX")

def solve(x,y,z):
    return min(len(concat(concat(x,y), z)), len(concat(x, concat(y,z))))

print(min(
    solve(a,b,c),
    solve(a,c,b),
    solve(b,a,c),
    solve(b,c,a),
    solve(c,a,b),
    solve(c,b,a)
))

Submission status: Wrong Answer

Solution code:
moji=[]

for i in range(3):
    moji.append(input())

gokei=len(moji[0])+len(moji[1])+len(moji[2])

data=[]



for i in range(3):
    for j in range(3):
        if(i==j):
            continue
        a=moji[i]
        b=moji[j]
        saidai=0
        #print(a,b)
        m=len(a)
        n=len(b)
        dekaiho=min(m,n)
        for k in range(dekaiho):
            bubun=[a[m+k-dekaiho:],b[:dekaiho-k]]
            #print(bubun)
            kano=True
            #print(dekaiho,k)
            for l in range(dekaiho-k):
                A=bubun[0][l]
                B=bubun[1][l]
                if(A!='?' and B!='?' and A!=B):
                    kano=False
                    break
            if(kano):
                saidai=dekaiho-k
                break
        data.append([i*10+j,saidai])

#print(data)

koho=[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

ans=[]

for k in koho:
    iti=(k[0]-1)*10+k[1]-1
    ni=(k[1]-1)*10+k[2]-1
    herasi=0
    for d in data:
        if(d[0]==iti or d[0]==ni):
            herasi+=d[1]
    ans.append(gokei-herasi)

print(min(ans))

Submission status: Wrong Answer

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

def calc(a,b):
    ans = ''
    for i in range(len(a)):
        len_ab = min(len(a)-i, len(b))
        tmp_a = a[i: i+len_ab]
        tmp_b = b[:len_ab]
        tmp = ''
        flag = 1
        for j in range(len_ab):
            if(tmp_a[j]==tmp_b[j]):
                tmp += tmp_a[j]
            elif(tmp_a[j]=='?'):
                tmp += tmp_b[j]
            elif(tmp_b[j]=='?'):
                tmp += tmp_a[j]
            else:
                flag = 0
                break
        if(flag == 1):
            ans = ans + tmp + a[i+len_ab:] + b[len_ab:]
            return(ans)
        else:
            ans += a[i]
    return(a+b)

ans = [0]*6
ans[0] = calc(calc(a,b),c)
ans[1] = calc(calc(a,c),b)
ans[2] = calc(calc(b,a),c)
ans[3] = calc(calc(b,c),a)
ans[4] = calc(calc(c,a),b)
ans[5] = calc(calc(c,b),a)

ans_n = len(ans[0])
for i in range(1,6):
    ans_n = min(ans_n, len(ans[i]))


print(ans_n)

Submission status: Wrong Answer

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


for i in range(len(a)):
    flag = True
    tmp = i
    for j in range(len(c)):
        if tmp >= len(a):
            break
        if a[tmp] == c[j]:
            tmp += 1
        elif a[tmp] == '?' or c[j] == '?':
            tmp += 1
        elif a[tmp] != c[j]:
            flag = False
            break

    if flag:
        for x in range(len(c)):
            if i+x >= len(a):
                a += c[x]
            if c[x] == '?':
                continue
            a[i+x] = c[x]
        break


for i in range(len(a)):
    flag = True
    tmp = i
    for j in range(len(b)):
        if tmp >= len(a):
            break
        if a[tmp] == b[j]:
            tmp += 1
        elif a[tmp] == '?' or b[j] == '?':
            tmp += 1
        elif a[tmp] != b[j]:
            flag = False
            break

    if flag:
        for x in range(len(b)):
            if i+x >= len(a):
                a += b[x]
            if b[x] == '?':
                continue
            a[i+x] = b[x]
        break

for i in range(len(a)):
    flag = True
    tmp = i
    for j in range(len(c)):
        if tmp >= len(a):
            break
        if a[tmp] == c[j]:
            tmp += 1
        elif a[tmp] == '?' or c[j] == '?':
            tmp += 1
        elif a[tmp] != c[j]:
            flag = False
            break

    if flag:
        for x in range(len(c)):
            if i+x >= len(a):
                a += c[x]
            if c[x] == '?':
                continue
            a[i+x] = c[x]
        break

print(len(a))

Submission status: Wrong Answer

Solution code:
def main():
    a = input()
    na = len(a)

    b = input()
    nb = len(b)

    c = input()
    nc = len(c)

    def intersect(p, q):
        if not p or not q:
            return 0

        lp = len(p)
        lq = len(q)

        head = q[0]
        cand = []
        for pi in range(lp):
            if head == '?' or p[pi] == '?' or p[pi] == head:
                cand.append((pi, lp - pi))

        for pi, ans in cand:
            if ans > lq: continue
            for pc, qc in zip(p[pi:], q):
                if not (pc == qc or pc == '?' or qc == '?'):
                    break
            else:
                return ans

        return 0

    def has(p, q):
        """pの中にq入ってる"""
        if not p or not q:
            return False

        lp = len(p)
        lq = len(q)

        head = q[0]
        cand = []
        for pi in range(lp):
            if head == '?' or p[pi] == '?' or p[pi] == head:
                if pi + lq - 1 < lp:
                    cand.append(pi)

        for pi in cand:
            for pc, qc in zip(p[pi:], q):
                if not (pc == qc or pc == '?' or qc == '?'):
                    break
            else:
                return True

        return False

    a_has_b = has(a, b)
    a_has_c = has(a, c)
    b_has_a = has(b, a)
    b_has_c = has(b, c)
    c_has_a = has(c, a)
    c_has_b = has(c, b)

    at_bh = intersect(a, b)
    bt_ch = intersect(b, c)
    at_ch = intersect(a, c)
    ct_bh = intersect(c, b)
    bt_ah = intersect(b, a)
    ct_ah = intersect(c, a)

    ret = na + nb + nc
    # res = [(at_bh + bt_ch),
    #        (at_ch + ct_bh),
    #        (bt_ah + at_ch),
    #        (bt_ch + ct_ah),
    #        (ct_ah + at_bh),
    #        (ct_bh + bt_ah)
    #        ]
    # print(ret, res)

    ret -= max(
        (at_bh + bt_ch),
        (at_ch + ct_bh),
        (bt_ah + at_ch),
        (bt_ch + ct_ah),
        (ct_ah + at_bh),
        (ct_bh + bt_ah)
    )

    if a_has_b:
        ret = min(ret, na + nc - at_ch)
        x1 = na + nc - at_ch
        ret = min(ret, na + nc - ct_ah)
        x2 = na + nc - ct_ah

    if a_has_c:
        ret = min(ret, na + nb - at_bh)
        x3 = na + nb - at_bh
        ret = min(ret, na + nb - bt_ah)
        x4 = na + nb - bt_ah

    if b_has_a:
        ret = min(ret, nb + nc - bt_ch)
        x5 = nb + nc - bt_ch
        ret = min(ret, nb + nc - ct_bh)
        x6 = nb + nc - ct_bh

    if b_has_c:
        ret = min(ret, na + nb - bt_ah)
        x7 = na + nb - bt_ah
        ret = min(ret, na + nb - at_bh)
        x8 = na + nb - at_bh

    if c_has_a:
        ret = min(ret, nb + nc - bt_ch)
        x9 = nb + nc - bt_ch
        ret = min(ret, nb + nc - ct_bh)
        x10 = nb + nc - ct_bh

    if c_has_b:
        ret = min(ret, na + nc - ct_ah)
        x11 = na + nc - ct_ah
        ret = min(ret, na + nc - at_ch)
        x12 = na + nc - at_ch

    if a_has_b and b_has_c:
        print(max(na, nb, nc))
        return

    if a_has_c and c_has_b:
        print(max(na, nb, nc))
        return

    if b_has_a and a_has_c:
        print(max(na, nb, nc))
        return

    if b_has_c and c_has_a:
        print(max(na, nb, nc))
        return

    if c_has_a and a_has_b:
        print(max(na, nb, nc))
        return

    if c_has_b and b_has_a:
        print(max(na, nb, nc))
        return

    print(ret)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

#     res = intersect('abc', 1, '', 1)
#     print(res)

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
  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:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(A, B, C):
    def f(a, b):
        x = INF
        for i in range(len(A)):
            for j in range(min(len(a)-i, len(b))):
                if not (a[i+j] == '?' or b[j] == '?' or a[i+j] == b[j]):
                    break
            else:
                x = i
                break

        y = INF
        for i in range(max(len(a)-len(b), 0), len(a)):
            for j in range(i, len(a)):
                if not (a[j] == '?' or b[j-i] == '?' or a[j] == b[j-i]):
                    break
            else:
                y = i
        return min(x, y)

    ans = len(A) + len(B) + len(C)
    for a, b, c in permutations([A, B, C]):
        ab = f(a, b)
        if ab == INF:
            s = a + b
        else:
            s = a[:ab] + b

        bc = f(s, c)
        if bc == INF:
            s += b
        else:
            s = s[:bc] + c

        ans = min(ans, len(s))

    return ans


def main():
    A = read_str()
    B = read_str()
    C = read_str()
    print(slv(A, B, C))

    # D = '?'*2000
    # print(slv(D, D, D))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

n=len(a)+len(b)+len(c)
x=[]

def comp(a,b):
    res=True
    for i in range(len(a)):
        if a[i]!=b[i] and a[i]!='?' and b[i]!='?':
            res=False
    return (res)

def comp2(a,b):
    if len(a)<len(b):
        i=len(a)
    else:
        i=len(b)
    while (not comp(a[len(a)-i:],b[0:i])) and i>0:
        i-=1
    return (i)

#abc
x.append (comp2(a,b)+comp2(b,c))
#acb
x.append (comp2(a,c)+comp2(c,b))
#bac
x.append (comp2(b,a)+comp2(a,c))
#bca
x.append (comp2(b,c)+comp2(c,a))
#cab
x.append (comp2(c,a)+comp2(a,b))
#cba
x.append (comp2(c,b)+comp2(b,a))

print(n-max(x))

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    from itertools import permutations
    input = sys.stdin.readline

    A = input().rstrip('\n')
    B = input().rstrip('\n')
    C = input().rstrip('\n')
    S = [A, B, C]

    ans = 10**5
    for abc in permutations(list(range(3))):
        a, b, c = S[abc[0]], S[abc[1]], S[abc[2]]
        na = len(a)
        nb = len(b)
        nc = len(c)

        flg = 0
        for i in range(na):
            s = [k for k in a[:i]]
            ok = 1
            for j in range(na-i):
                if j >= nb:
                    s.append(a[i+j])
                else:
                    #print(i+j, j)
                    if a[i+j] == b[j]:
                        s.append(b[j])
                    else:
                        if a[i+j] == '?':
                            s.append(b[j])
                        elif b[j] == '?':
                            s.append(a[i+j])
                        else:
                            ok = 0
            #print(s)
            if ok:
                flg = 1
                for k in range(na-i, nb):
                    s.append(b[k])
                break
        if not flg:
            s = [k for k in a+b]
        ns = len(s)

        flg = 0
        for i in range(ns):
            t = [k for k in s[:i]]
            ok = 1
            for j in range(ns-i):
                if j >= nc:
                    t.append(s[i+j])
                else:
                    if s[i+j] == c[j]:
                        t.append(c[j])
                    else:
                        if s[i+j] == '?':
                            t.append(c[j])
                        elif c[j] == '?':
                            t.append(s[i+j])
                        else:
                            ok = 0
            if ok:
                for k in range(ns-i, nc):
                    t.append(c[k])
                flg = 1
                break
        if not flg:
            ans = min(ans, ns + nc)
        else:
            ans = min(ans, len(t))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


for i in range(len(a)):
    flag = True
    tmp = i
    for j in range(len(c)):
        if tmp >= len(a):
            break
        if a[tmp] == c[j]:
            tmp += 1
        elif a[tmp] == '?' or c[j] == '?':
            tmp += 1
        elif a[tmp] != c[j]:
            flag = False
            break

    if flag:
        for x in range(len(c)):
            if i+x >= len(a):
                a += c[x]
            a[i+x] = c[x]
        break


for i in range(len(a)):
    flag = True
    tmp = i
    for j in range(len(b)):
        if tmp >= len(a):
            break
        if a[tmp] == b[j]:
            tmp += 1
        elif a[tmp] == '?' or b[j] == '?':
            tmp += 1
        elif a[tmp] != b[j]:
            flag = False
            break

    if flag:
        for x in range(len(b)):
            if i+x >= len(a):
                a += b[x]
            a[i+x] = b[x]
        break

for i in range(len(a)):
    flag = True
    tmp = i
    for j in range(len(c)):
        if tmp >= len(a):
            break
        if a[tmp] == c[j]:
            tmp += 1
        elif a[tmp] == '?' or c[j] == '?':
            tmp += 1
        elif a[tmp] != c[j]:
            flag = False
            break

    if flag:
        for x in range(len(c)):
            if i+x >= len(a):
                a += c[x]
            a[i+x] = c[x]
        break

print(len(a))

Submission status: Wrong Answer

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

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

def concat(x,y):
    len_x = len(x)
    len_y = len(y)
    for i in range(len_x+1):
        z = x[:i]
        ng = False
        for j in range(len_y):
            # log(x,y,i,j)
            if i+j >= len_x:
                z += y[j]
            elif x[i+j]=="?" or y[j]=="?" or x[i+j]==y[j]:
                if x[i+j]=="?" and y[j]=="?":
                    z += "?"
                else:
                    z += x[i+j] if x[i+j] != "?" else y[j]
            else:
                ng = True
                break
        if not ng:
            # log(x,y,z+x[i+j+1:])
            return z+x[i+j+1:]
    log("XXX")

def solve(x,y,z):
    return min(len(concat(concat(x,y), z)), len(concat(x, concat(y,z))))

print(min(
    solve(a,b,c),
    solve(a,c,b),
    solve(b,a,c),
    solve(b,c,a),
    solve(c,a,b),
    solve(c,b,a)
))

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
	return p

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=search(n1,n2)
	t=list(n1)
	tt=list(n2)
	if x:
		for i in range(x):
			if not t[-x+i]=="?":
				tt[i]=t[-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)