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
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
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)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def Z_algorithm(s):
        n = len(s)
        z = [0]*n
        z[0] = n
        c = 1
        for i in range(1,n):
            if i+z[i-c] < c+z[c]:
                z[i] = z[i-c]
            else:
                j = max(0,c+z[c]-i)
                while i+j < n and (s[j] == s[i+j] or s[i+j] == "?"):
                    j += 1
                z[i] = j
                c = i
        return z

    s = [input() for i in range(3)]
    ans = float("inf")
    for p in permutations(range(3)):
        a,b,c = s[p[0]],s[p[1]],s[p[2]]
        z = Z_algorithm(b+a)[len(b):]
        d = a+b
        zc = Z_algorithm(c+d)[len(c):]
        res = len(d)+len(c)
        for i in range(len(d)):
            if zc[i] >= len(d)-i:
                res = max(len(d),i+len(c))
                break
        for j in range(len(a)):
            if z[j] >= len(a)-j:
                d = a[:j]
                for i in range(len(b)):
                    k = i+j
                    if k < len(a) and b[i] == "?":
                            d += a[k]
                    else:
                        d += b[i]
            else:
                d = a+b
            zc = Z_algorithm(c+d)[len(c):]
            k = len(d)+len(c)
            for i in range(len(d)):
                if zc[i] >= len(d)-i:
                    k = max(len(d),i+len(c))
                    break
            if k < res:
                res = k
        if res < ans:
            ans = res
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

def equal(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))
  for i in range(1,length+1):
    if equal(s[:i], t[-i:])==False:
      return i-1
  return length

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
    

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

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 = a + b[common[0]:]
  if subset(tmp_str, c):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(tmp_str)+length[2]-max(common[4],common[3]))
  
  # a->c
  tmp_str = a + c[common[1]:]
  if subset(tmp_str, b):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(tmp_str)+length[1]-max(common[2],common[5]))
  
  # b->a
  tmp_str = b + a[common[2]:]
  if subset(tmp_str, c):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(tmp_str)+length[2]-max(common[5],common[1]))
  
  # b->c
  tmp_str = b + c[common[3]:]
  if subset(tmp_str, a):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(tmp_str)+length[0]-max(common[0],common[4]))
  
  # c->a
  tmp_str = c + a[common[4]:]
  if subset(tmp_str, b):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(tmp_str)+length[1]-max(common[3],common[0]))
  
  # c->b
  tmp_str = c + b[common[5]:]
  if subset(tmp_str, a):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(tmp_str)+length[0]-max(common[1],common[2]))
  
  return result

print(calculator())

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 f(a, b):
        m = []
        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:
                m.append((i))
        return m

    ans = len(A) + len(B) + len(C)
    N = len(A) + len(B) + len(C)
    for a, b, c in permutations([A, B, C]):
        ab = f(a, b)
        bc = f(b, c)
        # print(ab, bc)
        for x in ab:
            m = len(A) - x
            ans = min(ans, N-m)
            for y in bc:
                l = len(B) - y
                t = N - m - l
                ans = min(ans, t)


    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:
from itertools import permutations

def equal(s, t):
    ret = ''
    for i in range(len(s)):
        if s[i] == '?':
            ret += t[i]
        elif t[i] == '?' or s[i] == t[i]:
            ret += s[i]
        else:
            return ''
    return ret

def join(s, t):
    ret = [s + t]
    for i in range(len(s) - len(t)):
        res = equal(s[i:i+len(t)], t)
        if res:
            ret.append(s[:i] + res + s[i+len(t):])

    s_right = ''
    t_left = ''
    for i in range(min(len(s), len(t))):
        s_right = s[len(s)-i-1] + s_right
        t_left += t[i]
        res = equal(s_right, t_left)
        if res:
            ret.append(s[:len(s)-i-1] + res + t[i+1:])
            print(ret[-1])
            print(s_right, t_left)
    return ret


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

ans = float('inf')
for x, y, z in permutations([a, b, c]):
    xy = join(x, y)
    for s in xy:
        for t in join(s, z):
            ans = min(ans, len(t))
print(ans)

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

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

def match(a, b, start):
    l = float('inf')
    tmpi = 0
    for i in range(start, len(a)):
        aa = a[i:]
        if len(aa) < len(b):
            bb = b[:len(aa)]
        else:
            aa = aa[:len(b)]
            bb = b

        f = True
        for j in range(len(aa)):
            if aa[j] != bb[j] and aa[j] != '?' and bb[j] != '?':
                f = False
                break
            elif aa[j] == '?' and bb[j] != '?':
                aa[j] == bb[j]
            elif aa[j] != '?' and bb[j] == '?':
                bb[j] == aa[j]
        if f:
            if max(i + len(b), len(a)) < l:
                l = i + len(b)
                tmpi = i

    if l == float('inf'):
        return a + b
    else:
        if len(a[tmpi:]) < len(b):
            return a[:tmpi] + b
        else:
            return a

l = float('inf')
for seq in [(a, b, c), (a, c, b), (b, a, c), (b, c, a), (c, a, b), (c, b, a)]:
    s1 = match(seq[0], seq[1], 0)
    s2 = match(s1, seq[2], 0)
    l = min(l, len(s2))
print(l)

Submission status: Wrong Answer

Solution code:

a = input()
b = input()
c = input()
ans = 10**6

def h(s, t):
    for i in range(len(s)-1,-1,-1):
        if s[i] != "?" and t[i] == "?":
            return 1
        elif s[i] == "?" and t[i] != "?":
            return 0
    return 0

def e(s, t):
    ls = len(s)
    lt = len(t)
    ans = 10**6
    anss0 = []
    mh = 10**6
    for i in range(ls):
        ng = 0
        anss = []
        jj = 0
        for j in range(min(len(s)-i, len(t))):
            if s[i+j] != "?" and t[j] != "?" and\
                s[i+j] != t[j]:
                ng = 1
                break
            else:
                anss.append(s[i+j] if t[j]=="?" else t[j])
            jj = j
        jj += 1
        # print(anss)
        anss = list(s[:i]) + anss
        j = jj
        if len(s)-i != len(t):
            if j < len(s)-i:
                # print("t",i,j)
                anss += s[i+j:]
            else:
                # print("s",i,j)
                anss += t[j:]
        if ng==0 and (len(anss) < ans or \
            (len(anss)==ans and h(anss, anss0) == 1)):
            ans = len(anss)
            anss0 = anss[:]
    if ans==10**6:
        ans = len(s)+len(t)
        anss0 = list(s) + list(t)
    return anss0

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

Submission status: Wrong Answer

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

def equal(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))
  for i in range(1,length+1):
    if equal(s[:i], t[-i:])==False:
      return i-1
  return length

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
    

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

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 = a + b[common[0]:]
  if subset(tmp_str, c):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(tmp_str)+length[2]-max(common[4],common[5]))
  
  # a->c
  tmp_str = a + c[common[1]:]
  if subset(tmp_str, b):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(tmp_str)+length[1]-max(common[2],common[3]))
  
  # b->a
  tmp_str = b + a[common[2]:]
  if subset(tmp_str, c):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(tmp_str)+length[2]-max(common[4],common[5]))
  
  # b->c
  tmp_str = b + c[common[3]:]
  if subset(tmp_str, a):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(tmp_str)+length[0]-max(common[0],common[1]))
  
  # c->a
  tmp_str = c + a[common[4]:]
  if subset(tmp_str, b):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(tmp_str)+length[1]-max(common[2],common[3]))
  
  # c->b
  tmp_str = c + b[common[5]:]
  if subset(tmp_str, a):
    result = min(result, len(tmp_str))
  else:
    result = min(result, len(tmp_str)+length[0]-max(common[0],common[1]))
  
  return result

print(calculator())

Submission status: Wrong Answer

Solution code:
abc = [list(input()) for _ in range(3)]

import itertools

ans = 1000000007 # INF
for case in itertools.permutations(range(3)):
    for i, idx in enumerate(case):
        if i == 0:
            s_tmp = abc[idx]
            offset_prev = 0
        else:
            arg = abc[idx]
            for offset in range(offset_prev, len(s_tmp) + 1):
                for k in range(offset, min(len(s_tmp), offset + len(arg))):
                    if s_tmp[k] == '?' or arg[k - offset] == '?':
                        continue
                    elif s_tmp[k] == arg[k - offset]:
                        continue
                    else:
                        break
                else:
                    break
            #
            s_tmp_tmp = [None] * max(len(s_tmp), offset + len(arg))
            for pos in range(len(s_tmp_tmp)):
                if pos < offset:
                    s_tmp_tmp[pos] = s_tmp[pos]
                elif pos >= len(s_tmp):
                    s_tmp_tmp[pos] = arg[pos - offset]
                elif pos >= offset + len(arg):
                    s_tmp_tmp[pos] = s_tmp[pos]
                else:
                    if s_tmp[pos] == '?':
                        s_tmp_tmp[pos] = arg[pos - offset]
                    else:
                        s_tmp_tmp[pos] = s_tmp[pos]
            s_tmp = list(s_tmp_tmp)
            offset_prev = offset
    #
    # print("#", case, s_tmp)
    ans = min(ans, len(s_tmp))

print(ans)

Submission status: Wrong Answer

Solution code:
a=list(input())
b=list(input())
c=list(input())
answer=3*len(a)
def check(x,y,z):
    global answer
    con=0
    ans=[]
    a=0
    for i in range(len(x)):
        if x[i]==y[a] or y[a]=="?":
            a+=1
            con+=1
        else:
            a=0
            con=0
    ans.append(con)
    a=0
    con=0

    for i in range(len(y)):
        if y[i]==z[a] or z[a]=="?":
            a+=1
            con+=1
        else:
            a=0
            con=0
    ans.append(con)
    if 3*(len(x))-sum(ans)<answer:
        answer=3*(len(x))-sum(ans)

check(a,b,c)
check(a,c,b)
check(b,a,c)
check(b,c,a)
check(c,b,a)
check(c,a,b)

print(answer)

Submission status: Wrong Answer

Solution code:
from itertools import permutations
def main():
    C = [ input() for _ in range(3)]
    LC = [ len(c) for c in C]
    ans = sum(LC)
    for P in permutations( range(3)):
        now = sum(LC)
        a, b, c = P[0], P[1], P[2]
        X, Y, Z = C[a], C[b], C[c]
        x, y, z = LC[a], LC[b], LC[c]
        for i in range(x):
            # if x - i > y:
            #     continue
            check = True
            for j in range(i, min(x,i+y)):
                if X[j] == '?' or Y[j-i] == '?' or X[j] == Y[j-i]:
                    continue
                check = False
                break
            if check:
                if i+y <= x:
                    now -= y
                    y = x
                    Y = X
                else:
                    now -= x-i
                    Y = X[:i] + Y
                    y = now-z
                break
        else:
            Y = X+Y
        for i in range(y):
            check = True
            for j in range(i, min(y,i+z)):
                if Y[j] == '?' or Z[j-i] == '?' or Y[j] == Z[j-i]:
                    continue
                check = False
                break
            if check:
                if i+z <= y:
                    now -= z
                else:
                    now -= y-i
                break
        if now < ans:
            ans = now
    print(ans)
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def mix(s,t):
    for length in range(min(len(t),len(s)),0,-1):
        u=s[-length:]
        v=t[:length]

        #print(u,v)

        for i in range(length):
            if u[i]==v[i] or u[i]=="?" or v[i]=="?":
                continue
            else:
                break
        else:
            ret=""
            for i in range(length):
                if u[i]=="?":
                    ret+=v[i]
                else:
                    ret+=u[i]
                
            return s[:-length]+ret+t[length:]

    return s+t

ANS=[mix(mix(a,b),c),mix(mix(b,a),c),mix(mix(c,b),a),mix(mix(b,c),a),mix(mix(c,a),b),mix(mix(a,c),b),mix(c,mix(a,b)),mix(c,mix(b,a)),mix(a,mix(c,b)),mix(a,mix(b,c)),mix(b,mix(c,a)),mix(b,mix(a,c))]
L=1<<30
for a in ANS:
    L=min(L,len(a))
print(L)

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)
print(min(s-search(n1,n2)-search(n2,n3)for n1,n2,n3 in permutations((a,b,c),3)))

Submission status: Wrong Answer

Solution code:
import sys
import itertools
a, b, c = [c for c in input()], [c for c in input()], [c for c in input()]
m = None
def sub2(xy, i, z):
    if m is not None and max(len(xy), i + len(z)) >= m:
        return None
    #print('sub2',(xy, i, z))
    for j in range(i, min(len(xy), i + len(z))):
        if xy[j] != z[j - i] and xy[j] != '?' and z[j - i] != '?':
            return None
    return max(len(xy), i + len(z))
    
def sub1(x, y, i, z):
    if m is not None and max(len(x), i + len(y), i + len(z)) >= m:
        return None
    #print('sub1',(x, y, i, z))
    xy = x[:i] + ['?'] * max((len(x) - i), len(y))
    for j in range(i, min(len(x), i + len(y))):
        if x[j] == '?':
            xy[j] = y[j - i]
        elif y[j - i] == '?':
            xy[j] = x[i]
        elif x[j] != y[j - i]:
            return None
        else:
            xy[j] = x[j]
    for j in range(len(x), len(xy)):
        xy[j] = y[j - i]
    for k in range(i, len(xy)):
        v = sub2(xy, k, z)
        if v is not None:
            return v
    return len(xy) + len(z)

def sub(x, y, z):
    m = len(x) + len(y) + len(z)
    for i in range(len(x) + 1):
        v = sub1(x, y, i, z)
        if v is not None and v < m:
            m = v
    return m
for x, y, z in itertools.permutations([a, b, c]):
    v = sub(x, y, z)
    if m is None or v < m:
        m = v
print(m)

Submission status: Wrong Answer

Solution code:
from itertools import permutations
a = input()
b = input()
c = input()
ans = 0
for x, y, z in permutations([a, b, c]):
    for i in range(len(x)):
        cnt = 0
        for j in range(min(len(x) - i, len(y))):
            cnt += 1
            if x[j + i] != y[j] and x[j + i] != '?' and y[j] != '?':
                break
        else:
            t1 = cnt
            break
    else:
        t1 = 0
    for i in range(len(y)):
        cnt = 0
        for j in range(min(len(y) - i, len(z))):
            cnt += 1
            if y[j + i] != z[j] and y[j + i] != '?' and z[j] != '?':
                break
        else:
            t2 = cnt
            break
    else:
        t2 = 0
    ans = max(ans, t1 + t2)
print(len(a) + len(b) + len(c) - ans)

Submission status: Wrong Answer

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

b = "".join([x for x in b if x not in a and x != "?"])
c = "".join([x for x in c if x not in a and x not in b and x != "?"])

print(len(a+b+c))

Submission status: Wrong Answer

Solution code:
abc = [list(input()) for _ in range(3)]

import itertools

ans = 1000000007 # INF
for case in itertools.permutations(range(3)):
    for i, idx in enumerate(case):
        if i == 0:
            s_tmp = abc[idx]
        else:
            arg = abc[idx]
            for offset in range(len(s_tmp) + 1):
                for k in range(offset, min(len(s_tmp), offset + len(arg))):
                    if s_tmp[k] == '?' or arg[k - offset] == '?':
                        continue
                    elif s_tmp[k] == arg[k - offset]:
                        continue
                    else:
                        break
                else:
                    break
            #
            s_tmp_tmp = [None] * max(len(s_tmp), offset + len(arg))
            for pos in range(len(s_tmp_tmp)):
                if pos < offset:
                    s_tmp_tmp[pos] = s_tmp[pos]
                elif pos >= len(s_tmp):
                    s_tmp_tmp[pos] = arg[pos - offset]
                elif pos >= offset + len(arg):
                    s_tmp_tmp[pos] = s_tmp[pos]
                else:
                    if s_tmp[pos] == '?':
                        s_tmp_tmp[pos] = arg[pos - offset]
                    else:
                        s_tmp_tmp[pos] = s_tmp[pos]
            s_tmp = list(s_tmp_tmp)
    #
    # print("#", case, s_tmp)
    ans = min(ans, len(s_tmp))

print(ans)

Submission status: Wrong Answer

Solution code:
def judge(x, y):
    if len(x)!=len(y):
        return 0
    for i in range(len(x)):
        if x[i]!=y[i] and x[i]!='?' and y[i]!='?':
            return 0
    return 1

a=input()
b=input()
c=input()
ngs=max(len(a), len(b), len(c))

max1=0
max2=0
max3=0
max4=0
max5=0
max6=0

for i in range(ngs):
    ok=1
    if len(a)<i+1:
        ok=0
        break
    else:
        if len(b)<i+1:
            ok=0
            pass
        else:
            if judge(b[len(b)-(i+1):], a[:i+1])==1:
                max1=i+1
            else:
                ok=0
        if len(c)<i+1:
            ok=0
            pass
        else:
            if judge(c[len(c)-(i+1):], a[:i+1])==1:
                max2=i+1
            else:
                ok=0

for i in range(ngs):
    ok=1
    if len(b)<i+1:
        ok=0
        break
    else:
        if len(c)<i+1:
            ok=0
            pass
        else:
            if judge(c[len(c)-(i+1):], b[:i+1])==1:
                max3=i+1
            else:
                ok=0
        if len(a)<i+1:
            ok=0
            pass
        else:
            if judge(a[len(a)-(i+1):], b[:i+1])==1:
                max4=i+1
            else:
                ok=0

for i in range(ngs):
    ok=1
    if len(c)<i+1:
        ok=0
        break
    else:
        if len(a)<i+1:
            ok=0
            pass
        else:
            if judge(a[len(a)-(i+1):], c[:i+1])==1:
                max5=i+1
            else:
                ok=0
        if len(b)<i+1:
            ok=0
            pass
        else:
            if judge(b[len(b)-(i+1):], c[:i+1])==1:
                max6=i+1
            else:
                ok=0

#print(max1, max2, max3, max4, max5, max6)
hiku=max(max1+max3, max3+max5, max5+max1, max2+max4, max4+max6, max6+max2)
print(len(a)+len(b)+len(c)-hiku)

Submission status: Wrong Answer

Solution code:
def constructable(str1,str2):
    if str1=='?' or str2=='?':
        return True
    elif str1==str2:
        return True
    else:
        return False

def condition(num):
    N=num
    if N<max(len(a),len(b),len(c)):
        return False


    flag=0
    S=['?' for i in range(0,N)]
    for i in range(0,len(a)):
        S[i]=a[i]
    for i in range(0,len(c)):
        if not constructable(S[-i-1],c[-i-1]):
            flag=1
            break
        else:
            if S[-i-1]=='?':
                S[-i-1]=c[-i-1]
    if flag==0:
        for i in range(0,N-len(b)+1):
            start=i
            for j in range(0,len(b)):
                if not constructable(b[j],S[start+j]):
                    break
            else:
                return True

    flag=0
    S=['?' for i in range(0,N)]
    for i in range(0,len(a)):
        S[i]=a[i]
    for i in range(0,len(b)):
        if not constructable(S[-i-1],b[-i-1]):
            flag=1
            break
        else:
            if S[-i-1]=='?':
                S[-i-1]=b[-i-1]
    if flag==0:
        for i in range(0,N-len(c)+1):
            start=i
            for j in range(0,len(c)):
                if not constructable(c[j],S[start+j]):
                    break
            else:
                return True

    flag=0
    S=['?' for i in range(0,N)]
    for i in range(0,len(b)):
        S[i]=b[i]
    for i in range(0,len(c)):
        if not constructable(S[-i-1],c[-i-1]):
            flag=1
            break
        else:
            if S[-i-1]=='?':
                S[-i-1]=c[-i-1]
    if flag==0:
        for i in range(0,N-len(a)+1):
            start=i
            for j in range(0,len(a)):
                if not constructable(a[j],S[start+j]):
                    break
            else:
                return True

    flag=0
    S=['?' for i in range(0,N)]
    for i in range(0,len(b)):
        S[i]=b[i]
    for i in range(0,len(a)):
        if not constructable(S[-i-1],a[-i-1]):
            flag=1
            break
        else:
            if S[-i-1]=='?':
                S[-i-1]=a[-i-1]
    if flag==0:
        for i in range(0,N-len(c)+1):
            start=i
            for j in range(0,len(c)):
                if not constructable(c[j],S[start+j]):
                    break
            else:
                return True

    flag=0
    S=['?' for i in range(0,N)]
    for i in range(0,len(c)):
        S[i]=c[i]
    for i in range(0,len(a)):
        if not constructable(S[-i-1],a[-i-1]):
            flag=1
            break
        else:
            if S[-i-1]=='?':
                S[-i-1]=a[-i-1]
    if flag==0:
        for i in range(0,N-len(b)+1):
            start=i
            for j in range(0,len(b)):
                if not constructable(b[j],S[start+j]):
                    break
            else:
                return True

    flag=0
    S=['?' for i in range(0,N)]
    for i in range(0,len(c)):
        S[i]=c[i]
    for i in range(0,len(b)):
        if not constructable(S[-i-1],b[-i-1]):
            flag=1
            break
        else:
            if S[-i-1]=='?':
                S[-i-1]=b[-i-1]
    if flag==0:
        for i in range(0,N-len(a)+1):
            start=i
            for j in range(0,len(a)):
                if not constructable(a[j],S[start+j]):
                    break
            else:
                return True

    return False

a=input()
b=input()
c=input()
s=1
e=6000
while e-s>1:
    test=(e+s)//2
    if condition(test):
        e=test
    else:
        s=test

if condition(s):
    print(s)
else:
    print(e)

Submission status: Wrong Answer

Solution code:

def link(x,y): #x>=y前提

    for i in range(len(x)+1):

        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:
            #print (i)
            ret = []
            for j in range(max(len(x) , len(y)+i )):

                ret.append("?")
                if j < len(x):
                    ret[-1] = max(ret[-1] , x[j])
                if i <= j and j < i+len(y):
                    ret[-1] = max(ret[-1] , y[j-i])

            #print (x,y,"".join(ret))
            return ret


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

ans = float("inf")


ans = min( len(link(link(a,b),c)) , len(link(link(a,c),b)) , len(link(link(b,a),c)) , len(link(link(b,c),a)) , len(link(link(c,a),b)) , len(link(link(c,b),a)))
ans = min(ans , len(link(c,link(a,b))) , len(link(b,link(a,c))) , len(link(c,link(b,a))) , len(link(a,link(b,c))) , len(link(b,link(c,a))) , len(link(a,link(c,b))))

print (ans)

Submission status: Wrong Answer

Solution code:
from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import *
import sys
#input = sys.stdin.readline


#N=int(input())

#A=list(map(int,input().split()))
#XY=[list(map(int,input().split())) for i in range(N)]

D=[list(input()),list(input()),list(input())]

value=10**9
for a,b,c in permutations([0,1,2]):
    #print(a,b,c)

    a=copy.copy(D[a])
    b=copy.copy(D[b])
    c=copy.copy(D[c])

    for i in range(len(b)):
        if b[i]=="?" or c[0]=="?" or b[i]==c[0]:
            for m in range(min(len(b)-i-1,len(c)-1)):
                if b[i+1+m]=="?" or c[m+1]=="?" or b[i+1+m]==c[m+1]:
                    pass
                else:
                    break
            else:
                b=b[:i]+c
                break
    else:
        b=b+c


    for i in range(len(a)):
        if a[i]=="?" or b[0]=="?" or a[i]==b[0]:
            for m in range(min(len(a)-i-1,len(b)-1)):
                if a[i+1+m]=="?" or b[m+1]=="?" or a[i+1+m]==b[m+1]:
                    pass
                else:
                    break
            else:
                #print(a[:i],b,i)
                value=min(value,len(a[:i])+len(b))
                break
print(value)
#N=int(input())