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: Runtime Error

Solution code:
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

def Z_algo2(s):
    ls = len(s)
    lcp = [0]*ls
    lcp[0] = ls
    i,j = 1,0
    while i < ls:
        while i+j < ls and (s[j] == s[i+j] or s[j] == '?' or s[i+j] == '?'):
            j += 1
        lcp[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while i+k < ls and k + lcp[k] < j:
            lcp[i+k] = lcp[k]
            k += 1
        i += k
        j -= k
    return lcp

def Z_algo(s,t):
  l = [0]*len(t)
  for i in range(len(t)):
    j = 0
    while j < len(s) and i+j < len(t) and (s[j] == t[i+j] or s[j] == '?' or t[i+j] == '?'):
      j += 1
    l[i] = j
  return l

a = ns()
b = ns()
c = ns()

l = [a, b, c]
g = [[0]*3 for _ in range(3)]
for i in range(3):
    for j in range(3):
        if i != j:
            g[i][j] = Z_algo(l[i],l[j])
            # print(i,j,g[i][j])
ans = sum(len(x) for x in l)
for i in range(3):
    for j in range(3):
        if i == j: continue
        k = 3-i-j
        li = [ii for ii in range(len(l[i])) if g[j][i][ii] >= min(len(l[i]) - ii, len(l[j]))]
        lj = [ii for ii in range(len(l[j])) if g[k][j][ii] >= min(len(l[j]) - ii, len(l[k]))]
        for ii in li:
            for jj in lj:
                kk = ii + jj
                if kk >= len(l[k]) or g[k][i][kk] >= min(len(l[i]) - kk, len(l[k])):
                    c = max(len(l[i]), ii + len(l[j]), kk + len(l[k]))
                    if ans > c:
                        # print(i,j,k,ii,jj,c)
                        ans = c
print(ans)

Submission status: Runtime Error

Solution code:
s=[input() for i in range(3)]
l=[len(s[0]),len(s[1]),len(s[2])]
def ke(a,b,c):
    for i in range(l[a]+1):
        for j in range(l[a]-i):
            if s[a][i+j]!="?" and s[b][j]!="?" and s[a][i+j]!=s[b][j]:
                break
        else:
            t=i
            break
    for i in range(l[b]+1):
        for j in range(l[b]-i):
            if s[b][i+j]!="?" and s[c][j]!="?" and s[b][i+j]!=s[c][j]:
                break
        else:
            t+=i
            break
    return(l[a]+t)
print(min(ke(0,1,2),ke(0,2,1),ke(1,0,2),ke(1,2,0),ke(2,0,1),ke(2,1,0)))

Submission status: Runtime Error

Solution code:
def partial_match_table_mod(word):
    table = [0] * (len(word) + 1)
    table[0] = -1
    i, j = 0, 1

    while j < len(word):
        matched = (word[i] == word[j]) and (word[i] != "?")

        if not matched and i > 0:
            i = table[i]
        else:
            if matched:
                i += 1
            j += 1
            table[j] = i

    return table


def kmp_search_mod(string, pattern):
    table = partial_match_table_mod(pattern)
    i = j = 0
    while i < len(string) and j < len(pattern):
        if string[i] == pattern[j] or string[i] == "?" or pattern[j] == "?":
            i += 1
            j += 1
        elif j == 0:
            i += 1
        else:
            j = table[j]

    if j == len(pattern):
        return i - j
    else:
        return None


def solve_partial(s1, s2, s3):
    s1_add = s1 + "?" * len(s2)
    k1 = kmp_search_mod(s1_add, s2)
    # print(k1, s1_add, s2)
    assert k1 is not None
    s1_res = list(s1_add[:k1 + len(s2)])
    for i in range(k1, len(s1)):
        if s1[i] != s2[i - k1]:
            if s1[i] != "?":
                s1_res[i] = s1[i]
            else:
                s1_res[i] = s2[i - k1]
    for i in range(len(s1), k1 + len(s2)):
        s1_res[i] = s2[i - k1]
    s12 = "".join(s1_res)
    s12_add = s12 + "?" * len(s3)
    k2 = kmp_search_mod(s12_add, s3)
    assert k2 is not None
    return k2 + len(s3)


def solve(s1, s2, s3):
    res = len(s1) + len(s2) + len(s3)
    res = min(res, solve_partial(s1, s2, s3))
    res = min(res, solve_partial(s1, s3, s2))
    res = min(res, solve_partial(s2, s1, s3))
    res = min(res, solve_partial(s2, s3, s1))
    res = min(res, solve_partial(s3, s1, s2))
    res = min(res, solve_partial(s3, s2, s1))
    return res


def main():
    s1 = input()
    s2 = input()
    s3 = input()
    res = solve(s1, s2, s3)
    print(res)
    # for r in res:
    #     print(r)


def test():
    assert solve("a?c", "der", "cod") == 7
    assert solve("atcoder", "atcoder", "???????") == 7


if __name__ == "__main__":
    test()
    main()

Submission status: Runtime Error

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
C=[1]*n
Ans=[C]
cur=n-1
while cur:
  cur=0
  m=1
  for i in range(n-1):
    if C[i+1]<i+2:
      cur=i+1
  for i in range(cur):
    m=max(m,C[i])
  D=[]
  if C[cur]+1>m+1:
    cur-=1
  for i in range(n):
    if i<cur:
      D.append(C[i])
    elif i==cur:
      D.append(C[i]+1)
    else:
      D.append(1) 
  if cur!=0:
    Ans.append(D)
  C=Ans[-1]
for a in Ans:
  ans=""
  for i in range(n):
    ans+=chr(a[i]+96)
  print(ans)

Submission status: Runtime Error

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 y[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: Runtime Error

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

def mix(s,t):
    for length in range(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: Runtime Error

Solution code:
from functools import reduce
n = int(input())

alphabet = "abcdefghij"

cur = [[] for _ in range(10)]
nxt = [[] for _ in range(10)]

cur[0] = ["a"]
for _ in range(1, n):
	for i in range(10):
		for x in cur[i]:
			nxt[i].extend([x + alphabet[j] for j in range(i + 1)])
			nxt[i + 1].append(x + alphabet[i + 1])
	cur = nxt.copy()
	nxt = [[] for _ in range(10)]
ans = reduce(lambda a, b: a + b, cur)
ans.sort()
print(*ans, sep = "\n")

Submission status: Runtime Error

Solution code:
def solve(li):
    if len(li) == n:
        ans.append(li)
        return
    tmp = max(li)
    for i in range(1, tmp + 2):
        solve(li + [i])

ans = []
n = int(input())
solve([1])
res = []
alph = "abcdefghijklmnopq"

for ptn in ans:
    for i in range(n):
        ptn[i] = alph[ptn[i] - 1]
    res.append("".join(ptn))
res = sorted(res)
for i in res:
    print(i)

Submission status: Runtime Error

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

d = a+b+c
lt =[]
for ch in d:
  lt.append(ch)
print(len(set(lt))

Submission status: Runtime Error

Solution code:
a,b,c = input()
a = a+b+c
s = set()
for char in a:
  s.add(char)
  
print(len(s))

Submission status: Runtime Error

Solution code:
print "0"

Submission status: Runtime Error

Solution code:
a = "a?c"
b = "der"
c = "cod" 
print "7"