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:
A = input()
B = input()
C = input()
ans = len(A) + len(B) + len(C)
N = len(B) + len(C)

S = ('?' * N) + A + ('?' * N)

def makeB(left):
    T = list(S)

    for i, (s, t) in enumerate(zip(S[left:], B), start=left):
        if s == '?' or t == '?':
            T[i] = s if s != '?' else t
            continue
        if s != t:
            return False
    return ''.join(T[min(N, left): max(N + len(A), left + len(B))])

def isOk(T):
    for left in range(len(T) - len(C) + 1):
        for t, c in zip(T[left:], C):
            if t == '?' or c == '?':
                continue
            if t != c:
                break
        else:
            return True
    return False

T = []
for i in range(len(S) - len(B) + 1):
    t = makeB(i)
    if t:
        T.append(t)

print(ans)

Submission status: Wrong Answer

Solution code:
A = input()
B = input()
C = input()
ans = len(A) + len(B) + len(C)
N = len(B) + len(C)

S = ('?' * N) + A + ('?' * N)

def makeB(left):
    T = list(S)

    for i, (s, t) in enumerate(zip(S[left:], B), start=left):
        if s == '?' or t == '?':
            T[i] = s if s != '?' else t
            continue
        if s != t:
            return False
    return ''.join(T[min(N, left): max(N + len(A), left + len(B))])

def isOk(T):
    for left in range(len(T) - len(C) + 1):
        for t, c in zip(T[left:], C):
            if t == '?' or c == '?':
                continue
            if t != c:
                break
        else:
            return True
    return False

T = []
for i in range(len(S) - len(B) + 1):
    t = makeB(i)
    if t:
        T.append(t)

for t in T:
    if isOk(t):
        ans = min(ans, len(t))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import re

def chk(a,b,c):
      la = len(a)
      lb = len(b)
      lc = len(c)
      for x in range(0,la):
       xflag = 0
       for i in range(0,la-x):
         if not ( la > x+i and lb > i): 
           break
         if not ( a[x+i] == b[i] or a[x+i] == '?' or b[i] == '?'):
           xflag=1
           break
       if(xflag == 0):
         startb = i
         for y in range(0,lb):
           yflag = 0
           for j in range(0,lb-y):
             if not ( lb > y+j and lc > j): 
               break
             if not ( b[y+j] == c[j] or b[y+j] == '?' or c[j] == '?'):
               yflag=1
               break
           if(yflag == 0):
             startc = j
             if(la > x+y):
               for z in range(0,la):
                 zflag = 0
                 for k in range(0,la-z):
                   if not ( la > x+k and lc > k): 
                     break
                   if not ( a[x+k] == c[k] or a[x+k] == '?' or c[k] == '?'):
                     zflag=1
                     break
                 if(zflag == 0):
                   dummy = k
                   ret = a[0:x] + b[0:y] + c[z:]
                   return(len(ret))
             else:
               ret = a[0:x] + b[0:y] + c[0:]
               return(len(ret))
      return(0)


za = sys.stdin.readline().replace('\n','')
zb = sys.stdin.readline().replace('\n','')
zc = sys.stdin.readline().replace('\n','')

ret = chk(za,zb,zc)
if( ret != 0):
  print(ret)
  exit(0)
ret = chk(za,zc,zb)
if( ret != 0):
  print(ret)
  exit(0)
ret = chk(zb,za,zc)
if( ret != 0):
  print(ret)
  exit(0)
ret = chk(zb,zc,zb)
if( ret != 0):
  print(ret)
  exit(0)
ret = chk(zc,za,zb)
if( ret != 0):
  print(ret)
  exit(0)
ret = chk(zc,zb,za)
if( ret != 0):
  print(ret)
  exit(0)
sys.stdout.flush()

Submission status: Wrong Answer

Solution code:
import sys
import re

def chk(a,b,c):
      la = len(a)
      lb = len(b)
      lc = len(c)
      for x in range(0,la):
       xflag = 0
       for i in range(0,la-x):
         if( la > x+i and lb > i): 
           break
         if not ( a[x+i] == b[i] or a[x+i] == '?' or b[i] == '?'):
           xflag=1
           break
       if(xflag == 0):
         startb = i
         for y in range(0,lb):
           yflag = 0
           for j in range(0,lb-y):
             if( lb > y+j and lc > j): 
               break
             if not ( b[y+j] == c[j] or b[y+j] == '?' or c[j] == '?'):
               yflag=1
               break
           if(yflag == 0):
             startc = j
             if(la > x+y):
               for z in range(0,la):
                 zflag = 0
                 for k in range(0,la-z):
                   if( la > x+k and lc > k): 
                     break
                   if not ( a[x+k] == c[k] or a[x+k] == '?' or a[k] == '?'):
                     zflag=1
                     break
                 if(zflag == 0):
                   dummy = k
                   ret = a[0:x] + b[0:y] + c[z:]
                   return(len(ret))
             else:
               ret = a[0:x] + b[0:y] + c[0:]
               return(len(ret))
      return(0)


za = sys.stdin.readline().replace('\n','')
zb = sys.stdin.readline().replace('\n','')
zc = sys.stdin.readline().replace('\n','')

ret = chk(za,zb,zc)
if( ret != 0):
  print(ret)
  exit(0)
ret = chk(za,zc,zb)
if( ret != 0):
  print(ret)
  exit(0)
ret = chk(zb,za,zc)
if( ret != 0):
  print(ret)
  exit(0)
ret = chk(zb,zc,zb)
if( ret != 0):
  print(ret)
  exit(0)
ret = chk(zc,za,zb)
if( ret != 0):
  print(ret)
  exit(0)
ret = chk(zc,zb,za)
if( ret != 0):
  print(ret)
  exit(0)
sys.stdout.flush()

Submission status: Wrong Answer

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

    def check(x, y):
        for i in range(len(x)):
            for j in range(min(len(y), len(x)-i)):
                if x[i+j] != y[j] and (x[i+j] != "?" and y[j] != "?"):
                    break
            else:
                cnt = i
                break
        else:
            cnt = len(x)
        ans = x[:cnt]
        x_rest = x[cnt:]
        for i in range(max(len(x_rest), len(y))):
            if i >= len(x_rest):
                ans += y[i]
            elif i >= len(y):
                ans += x_rest[i]
            else:
                if x_rest[i] == "?":
                    ans += y[i]
                else:
                    ans += x_rest[i]
        return ans

    print(min([len(check(check(i, j), k)) for i, j, k in [
          (a, b, c), (a, c, b), (b, a, c), (b, c, a), (c, a, b), (c, b, a)]]))


main()

Submission status: Wrong Answer

Solution code:
import sys
import itertools
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

MX = 2005


def nd(i, j, k, s, d):
    if k >= len(s[i]):
        return True
    return d[i][j][k]


def main():
    s = [''] * 3
    s[0] = input().rstrip('\n')
    s[1] = input().rstrip('\n')
    s[2] = input().rstrip('\n')
    s.sort()
    ans = MX*3
    for x in itertools.permutations(s):
        d = [[[False for _ in range(MX)] for _ in range(3)] for _ in range(3)]
        for i in range(3):
            for j in range(i+1, 3):
                for k in range(len(s[i])):
                    check = True
                    for ni in range(k, len(s[i])):
                        nj = ni-k
                        if nj >= len(s[j]):
                            break
                        if s[i][ni] == '?' or s[j][nj] == '?':
                            continue
                        if s[i][ni] != s[j][nj]:
                            check = False
                            break
                    d[i][j][k] = check

        for x in range(MX):
            for y in range(MX):
                if not nd(0, 1, x, s, d):
                    continue
                if not nd(1, 2, y, s, d):
                    continue
                if not nd(0, 2, x+y, s, d):
                    continue
                now = max(len(s[0]), x+len(s[1]), x+y+len(s[2]))
                ans = min(ans, now)
    print(ans)


main()

Submission status: Wrong Answer

Solution code:

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

def smin(a,b):
    if a<b:
        return a
    else:
        return b

def smax(a,b):
    if a>b:
        return a
    else:
        return b

ab = [False]*100000
bc = [False]*100000
ac = [False]*100000

A,B,C = len(a),len(b),len(c)

def match(s1,s2):
    return s1=="?" or s2=="?" or s1==s2

for i in range(len(a)):
    for j in range(len(b)):
        ab[i-j+50000] = not match(a[i],b[j])

for i in range(len(a)):
    for j in range(len(c)):
        ac[i-j+50000] = not match(a[i],c[j])

for i in range(len(b)):
    for j in range(len(c)):
        bc[i-j+50000] = not match(b[i],c[j])

ans = 6000

for i in range(-4000,4001):
    for j in range(-4000,4001):
        if not ab[i+50000] and not ac[j+50000] and not bc[j-i+50000]:
            L = smin(0,smin(i,j))
            R = smax(A,smax(B+i,C+j))
            ans = smin(ans,R-L)

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):
    @lru_cache(maxsize=None)
    def g(x, y):
        m = []
        for i in range(len(x)):
            for j in range(min(len(y), len(x)-i)):
                if not (x[i+j] == '?' or y[j] == '?' or x[i+j] == y[j]):
                    break
            else:
                m.append(i)
        m.append(len(x))
        return m

    def f(x, y, z):
        N = len(x) + len(y) + len(z)
        ans = N
        xy = g(x, y)
        yz = g(y, z)
        xz = g(x, z)
        # print(xy, yz, xz)
        xz_set = set(xz)
        for i in xy:
            for j in yz:
                if i + j < len(x) and i + j in xz_set:
                    s = max(i + len(y), i + j + len(z), len(x))
                elif i + j < len(x) and i + len(y) :
                    l = bisect.bisect_left(xz, i+len(y))
                    if l >= len(xz):
                        continue
                    k = xz[l]
                    # print(k, xz)
                    s = max(i + len(y), k + len(z), len(x))
                elif  i + j >= len(x):
                    s = max(i + len(y), i + j + len(z), len(x))
                else:
                    continue
                # print(x, y, z, i, j, s)
                ans = min(ans, s)
        return ans

    ans = INF
    for a, b, c in permutations((A, B, C)):
        ans = min(ans, f(a, b, c))

    return ans


def ref(A, B, C):
    def f(x, y):
        for i in range(len(x)):
            for j in range(min(len(y), len(x)-i)):
                if not (x[i+j] == '?' or y[j] == '?' or x[i+j] == y[j]):
                    break
            else:
                if len(y) > len(x)-i:
                    s = x + y[len(x)-i:]
                    s = list(s)
                    for j in range(len(x)-i):
                        if (x[i+j] == '?' and y[j] != '?'):
                            s[i+j] = y[j]
                    yield ''.join(s)
                else:
                    s = list(x)
                    for j in range(len(y)):
                        if (x[i+j] == '?' and y[j] != '?'):
                            s[i+j] = y[j]
                    yield ''.join(s)
        yield x + y
    ans = INF
    for x, y, z in permutations((A, B, C)):
        # print('-----', x, y, z)
        for xy in f(x, y):
            for xyz in f(xy, z):
                # print(xyz, len(xyz), xy)
                ans = min(ans, len(xyz))
    return ans

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


    # A, B, C = ['b', 'cacdd', 'b']
    # A, B, C = ['d', 'acadcdac', 'ca']
    # A, B, C = ['ab?b', 'bb', 'bc?']
    # A, B, C = ['?aaa', 'b?ac', 'b?a?a']
    # A, B, C = ['acb', 'b??bc', 'cccbc?b']
    A, B, C = ['?bbbb', 'aa', 'cccba?ca']
    # A, B, C = ['abba', '?cb', 'ccacba?a']
    print(slv(A, B, C))
    print(ref(A, B, C))



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

    # for _ in range(10000):
    #     cs = 'abc?'
    #     A = ''.join(random.choices(cs, k=random.randint(1, 10)))
    #     B = ''.join(random.choices(cs, k=random.randint(1, 10)))
    #     C = ''.join(random.choices(cs, k=random.randint(1, 10)))
    #     a = slv(A, B, C)
    #     b = ref(A, B, C)
    #     if a != b:
    #         print(A)
    #         print(B)
    #         print(C)
    #         print(a, b)
    #         break


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

a=input()
b=input()
c=input()
anslst=[]
abc=[a,b,c]
for l in range(2):
    for m in range(2-l):
        a=abc[l]
        b=abc[l+m+1]
        # print(a,b)
        for k in range(2):
            lst=[0]
            for i in range(min(len(a),len(b))):
                # print(i)
                for j in range(i+1):
                    # print(a[-1-j],b[i-j])
                    if a[-1-j]=="?" or b[i-j]=="?":
                        continue
                    if a[-1-j]!=b[i-j]:
                        # lst.append(j)
                        break
                else:
                    lst.append(j+1)
            anslst.append(lst)
            a,b=b,a


# print(anslst)
tmp=0
lst=[[0,4],[1,5],[0,3],[2,1],[2,5],[3,4]]
for i in range(6):
    tmp=max(tmp,max(anslst[lst[i][0]])+max(anslst[lst[i][1]]))
# print(tmp)
print(len(abc[0])+len(abc[1])+len(abc[2])-tmp)

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):
    @lru_cache(maxsize=None)
    def g(x, y):
        m = []
        for i in range(len(x)):
            for j in range(min(len(y), len(x)-i)):
                if not (x[i+j] == '?' or y[j] == '?' or x[i+j] == y[j]):
                    break
            else:
                m.append(i)
        m.append(len(x))
        return m

    def f(x, y, z):
        N = len(x) + len(y) + len(z)
        ans = N
        xy = g(x, y)
        yz = g(y, z)
        xz = g(x, z)
        # print(xy, yz, xz)
        xz_set = set(xz)
        for i in xy:
            for j in yz:
                # print(i, j)
                if i + j < len(x) and i + j in xz_set:
                    s = max(i + len(y), i + j + len(z), len(x))
                elif i + j < len(x):
                    k = xz[bisect.bisect_left(xz, i+j+1)]
                    s = max(i + len(y), k + len(z), len(x))
                elif  i + j >= len(x):
                    s = max(i + len(y), i + j + len(z), len(x))
                else:
                    continue
                # print(x, y, z, i, j, s)
                ans = min(ans, s)
        return ans

    ans = INF
    for a, b, c in permutations((A, B, C)):
        ans = min(ans, f(a, b, c))

    return ans


def ref(A, B, C):
    def f(x, y):
        for i in range(len(x)):
            for j in range(min(len(y), len(x)-i)):
                if not (x[i+j] == '?' or y[j] == '?' or x[i+j] == y[j]):
                    break
            else:
                if len(y) > len(x)-i:
                    yield x[:i] + y
                else:
                    yield x
        yield x + y
    ans = INF
    for x, y, z in permutations((A, B, C)):
        # print('-----', x, y, z)
        for xy in f(x, y):
            for xyz in f(xy, z):
                # print(xyz, len(xyz), xy)
                ans = min(ans, len(xyz))
    return ans

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


    # A, B, C = ['b', 'cacdd', 'b']
    # # A, B, C = ['d', 'acadcdac', 'ca']
    # print(slv(A, B, C))
    # print(ref(A, B, C))



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

    # for _ in range(100):
    #     A = ''.join(random.choices('abcd', k=random.randint(1, 10)))
    #     B = ''.join(random.choices('abcd', k=random.randint(1, 10)))
    #     C = ''.join(random.choices('abcd', k=random.randint(1, 10)))
    #     a = slv(A, B, C)
    #     b = ref(A, B, C)
    #     if a != b:
    #         print(A)
    #         print(B)
    #         print(C)
    #         print(a, b)
    #         break


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[1]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)];rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()
 
def ran_input():
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True


a=input()
b=input()
c=input()
na,nb,nc=map(len,(a,b,c))

def check(a,b):
    na=len(a)
    nb=len(b)
    rt=[0]*na
    for i in range(na):
        fl=True
        for j in range(min(nb,na-i)):
            if (a[i+j]==b[j] or a[i+j]=='?' or b[j]=='?'):
                pass
            else:
                fl=False
                break
        if fl:
            rt[i]=1
    return rt+[1]

ab=check(a,b)
ba=check(b,a)
cb=check(c,b)
bc=check(b,c)
ac=check(a,c)
ca=check(c,a)

def tri_check(a,b,c,ab,bc,ac):
    na,nb,nc=map(len,(a,b,c))
    rt=na+nb+nc
    for l in range(na+1):
        for r in range(nb+1):
            if ab[l]==1 and bc[r]==1 and (l+r>=na or ac[l+r]==1):
                rt=min(rt,na+nb+nc-(na-l)-(nb-r))
    return rt

show_flg=False
z=a,b,c,ab,bc,ac;x=tri_check(*z);show(x,z)
ans=x
z=a,c,b,ac,cb,ab;x=tri_check(*z);show(x,z)
ans=min(ans,x)
z=b,a,c,ba,ac,bc;x=tri_check(*z);show(x,z)
ans=min(ans,x)
z=b,c,a,bc,ca,ba;x=tri_check(*z);show(x,z)
ans=min(ans,x)
z=c,a,b,ca,ab,cb;x=tri_check(*z);show(x,z)
ans=min(ans,x)
z=c,b,a,cb,ba,ca;x=tri_check(*z);show(x,z)
ans=min(ans,x)

print(ans)

Submission status: Wrong Answer

Solution code:
s = {}
s[0] = input()
s[1] = input()
s[2] =input()

def compe(a,b):
    a,b = list(a),list(b)
    L = len(a) if len(a) < len(b) else len(b)
    c = 0
    for i in range(L):
        flg = 0
        for j,(x,y) in enumerate(zip(a[len(a) - i -1:len(a)],b[0:i+1])):
            if x == '?' or y == '?':
                continue
            elif x == y:
                continue
            else:
                flg = 1
                break
        if flg == 0:
            c = i+1
    ab_1 = a + b[c:]
    ab_2 = a[0:len(a)-c] + b
    for i ,(x, y) in enumerate(zip(ab_1,ab_2)):
        if x == '?':
            ab_1[i] = y
    return c,ab_1

junban = [[0,1,2],[0,2,1],[1,0,2],[1,2,0],[2,0,1],[2,1,0]]
min_s = 100000
for j in junban:
    p,obj = compe(s[j[0]],s[j[1]])
    q,obj2 = compe(obj,s[j[2]])
    L = len(obj2)
    if min_s > L:
        min_s = L

print(min_s)

Submission status: Wrong Answer

Solution code:
s = {}
s[0] = input()
s[1] = input()
s[2] =input()

def compe(a,b):
    a,b = list(a),list(b)
    L = len(a) if len(a) < len(b) else len(b)
    c = 0
    for i in range(L):
        flg = 0
        for j,(x,y) in enumerate(zip(a[len(a) - i -1:len(a)],b[0:i+1])):
            if x == '?' or y == '?':
                continue
            elif x == y:
                continue
            else:
                flg = 1
                break
        if flg == 0:
            c = i+1
    ab_1 = a + b[c:]
    ab_2 = a[0:len(a)-c] + b
    for i ,(x, y) in enumerate(zip(ab_1,ab_2)):
        if x == '?':
            ab_1[i] = y
    return c,ab_1

junban = [[0,1,2],[0,2,1],[1,0,2],[1,2,0],[2,0,1],[2,1,0]]
min_s = 100000
for j in junban:
    p,obj = compe(s[j[0]],s[j[1]])
    q,obj2 = compe(obj,s[j[2]])
    L = len(s[0])+len(s[1])+len(s[2]) - p - q
    if min_s > L:
        min_s = L

print(min_s)

Submission status: Wrong Answer

Solution code:
a = [input(),input(),input()]
def checkEqual(l,r,k):
    i = len(l)-k
    j = 0
    end = min(k,len(r))
    for a in range(end):
        x, y = i+a, j+a
        if l[x] != r[y]:
            if l[x]=='?' or r[y]=='?':
                continue
            return False
    return True

overLaps = [[[] for i in range(3)] for j in range(3)]
ij = [(0,1),(1,0),(1,2),(2,1),(0,2),(2,0)]
for i,j in ij:
    l = a[i]
    r = a[j]
    for k in range(len(l)+1):
        if checkEqual(l,r,k):
            overLaps[i][j].append(k)

ans = len(a[0])+len(a[1])+len(a[2])
combs = [(0,1,2),(0,2,1),(1,0,2),(1,2,0),(2,0,1),(2,1,0)]
for x,y,z in combs:
    l0,l1,l2 = len(a[x]), len(a[y]), len(a[z])
    o0 = overLaps[x][y]
    o2 = overLaps[y][z]
    midOl = set(overLaps[x][z])
    for i in o0:
        for j in o2:
            if i+j<=l1:
                newLen = l0+l1+l2-i-j
            elif i+j-l1 in midOl:
                newLen = max(l0, l0+l1-i, l0+l1+l2-i-j)
            ans = min(newLen,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import permutations
import sys

def SI(): return sys.stdin.readline()[:-1]

def main():
    def same(x,y):
        xn,yn=len(x),len(y)
        # x,yの一致する位置を調べる
        res = [True] * 4005
        for i in range(xn):
            for c0, c1 in zip(x[i:], y):
                if c0 != q and c1 != q and c0 != c1:
                    res[i] = False
                    break
        return res

    q=ord("?")
    ss=[[ord(c) for c in SI()] for _ in range(3)]
    # 一致を調べる
    eq=[[0]*3 for _ in range(3)]
    for i in range(3):
        for j in range(3):
            if i==j:continue
            eq[i][j]=same(ss[i],ss[j])
    ans=sum(len(ss[i]) for i in range(3))
    for si,sj,sk in permutations(range(3)):
        for i in range(len(ss[si])):
            for j in range(len(ss[sj])):
                if eq[si][sj][i] and eq[sj][sk][j] and eq[si][sk][i+j]:
                    cur=max(len(ss[si]),len(ss[sj])+i,len(ss[sk])+i+j)
                    ans=min(ans,cur)
    print(ans)

main()

Submission status: Wrong Answer

Solution code:
s = {}
s[0] = input()
s[1] = input()
s[2] =input()

def compe(a,b):
    a,b = list(a),list(b)
    L = len(a) if len(a) < len(b) else len(b)
    c = 0
    for i in range(L):
        flg = 0
        for x,y in zip(a[len(a) - i -1:len(a)],b[0:i+1]):
            if x == '?' or y == '?':
                continue
            elif x == y:
                continue
            else:
                flg = 1
                break
        if flg == 0:
            c = i+1
    return c

junban = [[0,1,2],[0,2,1],[1,0,2],[1,2,0],[2,0,1],[2,1,0]]
min_s = 100000
for j in junban:
    p = compe(s[j[0]],s[j[1]])
    q = compe(s[j[1]],s[j[2]])
    L = len(s[0])+len(s[1])+len(s[2]) - p - q
    if min_s > L:
        min_s = L

print(min_s)

Submission status: Wrong Answer

Solution code:
s = {}
s[0] = input()
s[1] = input()
s[2] =input()

def compe(a,b):
    a,b = list(a),list(b)
    L = len(a) if len(a) < len(b) else len(b)
    c = 0
    for i in range(len(a)):
        flg = 0
        for x,y in zip(a[len(a) - i -1:len(a)],b[0:i+1]):
            if x == '?' or y == '?':
                continue
            elif x == y:
                continue
            else:
                flg = 1
                break
        if flg == 0:
            c = i+1
    return c

junban = [[0,1,2],[0,2,1],[1,0,2],[1,2,0],[2,0,1],[2,1,0]]
min_s = 100000
for j in junban:
    p = compe(s[j[0]],s[j[1]])
    q = compe(s[j[1]],s[j[2]])
    L = len(s[0])+len(s[1])+len(s[2]) - p - q
    if min_s > L:
        min_s = L

print(min_s)

Submission status: Wrong Answer

Solution code:
a = [input(),input(),input()]
def checkEqual(l,r,k):
    i = len(l)-k
    j = 0
    for a in range(k):
        x, y = i+a, j+a
        if l[x] != r[y]:
            if l[x]=='?' or r[y]=='?':
                continue
            return False
    return True

overLaps = [[[] for i in range(3)] for j in range(3)]
ij = [(0,1),(1,0),(1,2),(2,1),(0,2),(2,0)]
for i,j in ij:
    l = a[i]
    r = a[j]
    m = min(len(l),len(r))
    for k in range(m+1):
        if checkEqual(l,r,k):
            overLaps[i][j].append(k)

maxRe = 0
combs = [(0,1,2),(0,2,1),(1,0,2),(1,2,0),(2,0,1),(2,1,0)]
for x,y,z in combs:
    full = len(a[y])
    o1 = overLaps[x][y]
    o2 = overLaps[y][z]
    midOl = set(overLaps[x][z])
    for i in o1:
        for j in o2:
            re = i+j
            if re<=full:
                maxRe = max(re, maxRe)
            elif re-full in midOl:
                maxRe = max(re, maxRe)

ans = len(a[0])+len(a[1])+len(a[2])-maxRe
print(ans)