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:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define rep3(i, s, n) for (int i = s; i > (int)(n); i--)
#define all(obj) obj.begin(), obj.end()
#define db(x) cerr << #x << ":" << x << " "
#define dbl(x) cerr << #x << ":" << x << "\n"
#define dbv(vec) cerr << #vec << ":"; for (auto e : vec) cerr << e << " "; cout << "\n"
#define dbvv(vv) cerr << #vv << ":\n"; for (auto vec : vv) { for (auto e : vec) cerr << e << " "; cerr << endl; }
#define YN(f) cout << (f ? "YES" : "NO") << endl
#define Yn(f) cout << (f ? "Yes" : "No") << endl
#define yn(f) cout << (f ? "yes" : "no") << endl
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;

bool canput(string s1, string s2, int d) {
	int l1 = s1.size(), l2 = s2.size();
	rep2(i, d, min(l1, d + l2)) {
		if (s1.at(i) != '?' && s2.at(i - d) != '?' && s1.at(i) != s2.at(i - d)) {
			return false;
		}
	}
	return true;
}

int ans;

void solve(string s1, string s2, string s3) {
	int l1 = s1.size(), l2 = s2.size(), l3 = s3.size();
	rep(d12, l1 + 1) {
		dbl(d12);
		dbl(canput(s1, s2, d12));
		if (!canput(s1, s2, d12)) continue;
		rep(d23, l2 + 1) {
			dbl(d23);
			db(canput(s2, s3, d23)); dbl(canput(s1, s3, d12 + d23));
			if (canput(s2, s3, d23) && canput(s1, s3, d12 + d23)) {
				ans = min(ans, max({ l1, d12 + l2, d12 + d23 + l3 }));
			}
		}
	}
}

int main() {
	string a, b, c; cin >> a >> b >> c;
	int la = a.size(), lb = b.size(), lc = c.size();
	ans = la + lb + lc;

	solve(a, b, c);
	solve(a, c, b);
	solve(b, a, c);
	solve(b, c, a);
	solve(c, a, b);
	solve(c, b, a);

	cout << ans << endl;
}

Submission status: Runtime Error

Solution code:
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define MP make_pair
#define EACH(i,c) for(auto i: c)
#define SORT(c) sort((c).begin(),(c).end())

#define ALL(a)  (a).begin(),(a).end()

string solve_sub(const string &a, const string &b)
{
	int n = a.size();
	int m = b.size();
	int x = n;
	REP(i, n){
		int l = min(n - i, m);
		bool ok = true;
		REP(j, l){
			if(a[i + j] != b[j] && a[i + j] != '?' && b[j] != '?'){
				ok = false;
				break;
			}
		}
		if(ok){
			x = i;
			break;
		}
	}
	// cerr << x << endl;
	string c(m + x, '?');
	REP(i, m + x){
		int j = i - x;
		if(j < 0) c[i] = a[i];
		else if(i < n && j < m) c[i] = a[i] != '?' ? a[i] : b[j];
		else if(i < n) c[i] = a[i];
		else c[i] = b[j];
	}
	return c;
}

int solve(const string &a, const string &b, const string &c)
{
	string d = solve_sub(a, b);
	string e = solve_sub(d, c);
	// cerr << d << endl;
	// cerr << e << endl;
	return e.size();
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	string a, b, c;
	cin >> a >> b >> c;

	int ret = 1e9;

	ret = min(ret, solve(a, b, c));
	ret = min(ret, solve(a, c, b));
	ret = min(ret, solve(b, a, c));
	ret = min(ret, solve(b, c, a));
	ret = min(ret, solve(c, a, b));
	ret = min(ret, solve(c, b, a));

	cout << ret << endl;

	return 0;
}

Submission status: Runtime Error

Solution code:
a = input()
b = input()
c = input()
def eq(a,b):
    for i in range(len(a)):
        if a[i] == b[i] or a[i] == '?' or b[i] == '?':
            continue
        else:
            return False
    return True

def check(a,b):
    if a<b:
        return True
    else:
        return False

def conbine(a,b,n):
    min = 100000000000
    merged = ''
    for i in range(n):
        if a[i] == b[0] or a[i] == '?' or b[0] == '?':
            if eq(a[i:], b[:n-i+1]):
                if check(i+len(b), min):
                    min = i+len(b)
                    merged = a[:i] + b
    if check(len(a)+len(b),min):
        min = len(a)+len(b)
        merged = a + b
    return [min, merged]

def t(a,b,c):
    ans1 = conbine(a,b, len(a))
    ans3 = conbine(ans1[1], c, len(ans1[1]))
    return ans3[0]

R = [t(a,b,c), t(a,c,b), t(b,a,c), t(b,c,a), t(c,a,b), t(c,b,a)]

print(min(R))

Submission status: Runtime Error

Solution code:

def main():

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

    def LCS(S, T):

        if len(S) == 0 or len(T) == 0:
            return 0
        

        dp = [[0 for _ in range(len(S) + 1)] for _ in range(len(T) + 1)]
        for i in range(len(S)):
            for j in range(len(T)):
                if S[i] == "?" or T[j] == "?" or S[i] == T[j]:
                    dp[i+1][j+1] = dp[i][j] + 1
                else:
                    dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])

        return dp[len(S)][len(T)]

    ans = 0

    ab = LCS(a,b)

    ac = LCS(a,c)

    bc = LCS(b,c)


    if ab == ac == bc == 0:
        print(len(a) + len(b) + len(c))
        exit()

    if ac <= ab and ac <= bc:
        ans = (len(a) + len(b) - ab) + (len(c) - bc) 

    elif ab <= ac and ab <= bc:
        ans = (len(a) + len(c) - ac) + (len(c) - bc) 

    elif bc <= ab and bc <= ac:
        ans = (len(a) + len(b) - ab) + (len(c) - ac)

    print(ans) 


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
a,b,c = input(),input(),input()
ma = len(a)+len(b)+len(c)
m = ma
def check1(i,j,l):
  f = 0
  for n in range(l):
    if i[len(i)-l+n] != j[n] and i[len(i)-l+n] != '?' and j[n] != '?':
      f = 1
      break
  if f == 0:
    return True
  else:
    return False
def check2(i,j,k,o):
  f = 0
  for n in range(o):
    if j[len(j)-o+n] != k[n]:
      if k[n] == '?':
        continue
      elif j[len(j)-o+n] == '?':
        try:
          if i[len(i)-l+len(j)-o+n] == k[n] or i[len(i)-l+len(j)-o+n] == '?':
            continue
          else:
            f = 1
            break
        except:
          continue
      else:
        f = 1
        break
  if f == 0:
    return True
  else:
    return False
for i,j,k in [[a,b,c],[a,c,b],[b,a,c],[b,c,a],[c,a,b],[c,b,a]]:
  for o in range(len(k),-1,-1):
    for l in range(len(j),-1,-1):
      if check1(i,j,l):
        if check2(i,j,k,o):
          m = min(m,ma-l-o)
print(m)

Submission status: Runtime Error

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

Solution code:

def main():

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

    def LCS(S, T):
        dp = [[0 for _ in range(len(S) + 1)] for _ in range(len(T) + 1)]
        for i in range(len(S)):
            for j in range(len(T)):
                if S[i] == "?" or T[j] == "?" or S[i] == T[j]:
                    dp[i+1][j+1] = dp[i][j] + 1
                else:
                    dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])

        return dp[len(S)][len(T)]

    ans = 0

    ab = LCS(a,b)

    ac = LCS(a,c)

    bc = LCS(b,c)


    if ab == ac == bc == 0:
        print(len(a) + len(b) + len(c))
        exit()

    if ac <= ab and ac <= bc:
        ans = (len(a) + len(b) - ab) + (len(c) - bc) 

    elif ab <= ac and ab <= bc:
        ans = (len(a) + len(c) - ac) + (len(c) - bc) 

    elif bc <= ab and bc <= ac:
        ans = (len(a) + len(b) - ab) + (len(c) - ac)

    print(ans) 


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

Solution code:



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

def LCS(S, T):
    dp = [[0 for _ in range(len(S) + 1)] for _ in range(len(T) + 1)]
    for i in range(len(S)):
        for j in range(len(T)):
            if S[i] == "?" or T[j] == "?" or S[i] == T[j]:
                dp[i+1][j+1] = dp[i][j] + 1
            else:
                dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])

    return dp[len(S)][len(T)]

ans = 0

ab = LCS(a,b)

ac = LCS(a,c)

bc = LCS(b,c)


if ab == ac == bc == 0:
    print(len(a) + len(b) + len(c))
    exit()

if ac <= ab and ac <= bc:
    ans = (len(a) + len(b) - ab) + (len(c) - bc) 

elif ab <= ac and ab <= bc:
    ans = (len(a) + len(c) - ac) + (len(c) - bc) 

elif bc <= ab and bc <= ac:
    ans = (len(a) + len(b) - ab) + (len(c) - ac)

print(ans)

Submission status: Runtime Error

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=max(m,n)
        for k in range(dekaiho):
            bubun=[a[m+k-dekaiho:],b[:dekaiho-k]]
            kano=True
            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: Runtime Error

Solution code:
a = list(input())
b = list(input())
c = list(input())
def f(a, b):
    nab = 0
    for i in range(len(a)):
        flag = True
        for j in range(min(len(b),len(a)-i)):
            if a[i+j] != '?' and b[j] != '?' and a[i+j] != b[j]:
                flag = False
        if flag:
            if len(b) <= len(a)-i:
                nab = -i-1
            else:
                nab = len(a)-i
            break
    if nab < 0:
        ab = ['?'] * len(a)
        for i in range(len(a)):
            ab[i] = a[i] if a[i] != '?' else b[i+1+nab]
    else:
        ab = ['?'] * (len(a)+len(b)-nab)
        for i in range(len(ab)):
            if i < len(a)-nab:
                ab[i] = a[i]
            elif len(a)-nab<=i<len(a):
                ab[i] = a[i] if a[i] != '?' else b[i+nab-len(a)]
            else:
                ab[i] = b[i+nab-len(a)]
    return ab
print(min(len(f(f(a,b),c)),len(f(f(b,a),c)),len(f(f(a,c),b)),len(f(f(b,c),a)),len(f(f(c,a),b)),len(f(f(c,b),a))))

Submission status: Runtime Error

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
        for j in range(p - i):
            if a[j + i] != "?" and b[j] != "?" and a[j + i] != b[j]:
                break
            f += 1
        if f == p - i:
            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)
print(min(res))

Submission status: Runtime Error

Solution code:
a = list(input())
b = list(input())
c = list(input())
n0 = len(a)
n1 = len(b)
n2 = len(c)
al = []
def mae(a,b):
    n0 = len(a)
    n1 = len(b)
    al = []
    now = []
    count = 0
    for i in range(n1):
        if a[0] == b[i] or a[0] == "?" or b[i] == "?":
            now2 = now
            count2 = 0
            for j in range(min(n1-i,n0)):
                if a[j] != b[j+i] and a[j] != "?" and b[j+i] != "?":
                    break
                else:
                    count2 += 1
                    now2.append(a[j])
            count = max(count,count2)
            al.append([now2,count2])
        now.append(b[i])
    return al,count
def ushiro(a,b):
    n0 = len(a)
    n1 = len(b)
    al = []
    now = []
    count = 0
    for i in range(n1-1,-1,-1):
        if a[n0-1] == b[i] or a[n0-1] == "?" or b[i] == "?":
            now2 = now
            count2 = 0
            for j in range(min(i,n0)):
                if a[n0-1-j] != b[i-j] and a[n0-1-j] != "?" and b[i-j] != "?":
                    break
                else:
                    count2 += 1
                    now2.append(a[j])
            count = max(count,count2)
            al.append([now2,count2])
        now.append(b[i])
    return al,count
ansl = []
c1,k = mae(a,b)
c2,p = ushiro(a,b)
for i in c1:
    c3,k1 = mae(c,i[0])
    ansl.append(k1+i[1])
for i in c2:
    c3,k1 = ushiro(c,i[0])
    ansl.append(k1+i[1])
c1,k = mae(b,a)
c2,p = ushiro(b,a)
for i in c1:
    c3,k1 = mae(c,i[0])
    ansl.append(k1+i[1])
for i in c2:
    c3,k1 = ushiro(c,i[0])
    ansl.append(k1+i[1])
c1,k = mae(a,c)
c2,p = ushiro(a,c)
for i in c1:
    c3,k1 = mae(b,i[0])
    ansl.append(k1+i[1])
for i in c2:
    c3,k1 = ushiro(b,i[0])
    ansl.append(k1+i[1])
c1,k = mae(c,a)
c2,p = ushiro(c,a)
for i in c1:
    c3,k1 = mae(b,i[0])
    ansl.append(k1+i[1])
for i in c2:
    c3,k1 = ushiro(b,i[0])
    ansl.append(k1+i[1])
c1,k = mae(c,b)
c2,p = ushiro(c,b)
for i in c1:
    c3,k1 = mae(a,i[0])
    ansl.append(k1+i[1])
for i in c2:
    c3,k1 = ushiro(a,i[0])
    ansl.append(k1+i[1])
c1,k = mae(b,c)
c2,p = ushiro(b,c)
for i in c1:
    c3,k1 = mae(a,i[0])
    ansl.append(k1+i[1])
for i in c2:
    c3,k1 = ushiro(a,i[0])
    ansl.append(k1+i[1])
print(n0+n1+n2-max(ansl))

Submission status: Runtime Error

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

arr = [a,b,c]

import itertools

#p = itertools.permutations(arr, 3)
ans = []
for v in itertools.permutations(arr, 3):
    a = list(v[0])
    b = list(v[1])
    c = list(v[2])

    sa = 0
    ea = min(len(a),len(b))
    sb = 0
    eb = min(len(a),len(b))
    while(True):
        tail = list(a[sa:ea])
        head = list(b[sb:eb])
        for i in range(len(tail)):
            if tail[i] == '?':
                tail[i] = head[i]
        for i in range(len(head)):
            if head[i] == '?':
                head[i] = tail[i]

        if tail == head or sa == ea or sb == eb:
            a = a[:sa]
            break
        else:
            sa += 1
            eb -= 1

    sb = 0
    eb = min(len(b),len(c))
    sc = 0
    ec = min(len(c),len(c))
    while(True):
        tail = list(b[sb:eb])
        head = list(c[sc:ec])
        for i in range(len(tail)):
            if tail[i] == '?':
                tail[i] = head[i]
        for i in range(len(head)):
            if head[i] == '?':
                head[i] = tail[i]

        if tail == head or sb == eb or sc == ec:
            b = b[:sb]
            break
        else:
            sb += 1
            ec -= 1

    ans.append(len(a)+len(b)+len(c))

print(min(ans))

Submission status: Runtime Error

Solution code:
import sys
import itertools
a, b, c = [c for c in input()], [c for c in input()], [c for c in input()]
def sub2(xy, i, z, m):
    newm = max(len(xy), i + len(z))
    if newm >= m:
        return m
    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 m
    return newm
    
def sub1(x, y, i, z, m):
    if max(len(x), i + len(y), i + len(z)) >= m:
        return m
    xy = x + y[len(x) - i:]
    for j in range(i, min(len(x), i + len(y))):
        if x[j] != y[j - i]:
            if x[j] == '?':
                if y[j - i] != '?':
                    xy[j] = y[j - i]
            elif y[j] != '?':
                return m
    for k in range(i, len(xy)):
        m = sub2(xy, k, z, m)
    return m

def sub(x, y, z, m):
    for i in range(len(x) + 1):
        m = sub1(x, y, i, z, m)
    return m
m = len(a) + len(b) + len(c)
for x, y, z in itertools.permutations([a, b, c]):
    m = sub(x, y, z, m)
print(m)

Submission status: Runtime Error

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

def compare(a, b, i):
	min_len = min(len(a[i:]), len(b))
	out = a[:i]
	for j in range(min_len):
		if a[i:][j] == b[j]:
			if a[i:][j] != "?":
				out += a[i:][j]
			elif b[j] != "?":
				out += b[j]
			else:
				out += "?"
		else:
			return False
	return out + b[min_len:]

ab = set()
for i in range(len(a) + 1):
	if compare(a, b, i):
		ab.add(compare(a, b, i))

for i in range(len(b) + 1):
	if compare(b, a, i):
		ab.add(compare(b, a, i))

ab = list(ab)

bc = set()
for i in range(len(b) + 1):
	if compare(b, c, i):
		bc.add(compare(b, c, i))

for i in range(len(c) + 1):
	if compare(c, b, i):
		bc.add(compare(c, b, i))

bc = list(bc)


print(ab)
ans = 10000
for d in ab:
	for i in range(len(d) + 1):
		if compare(d, c, i):
			ans = min(ans, len(compare(d, c, i)))

	for i in range(len(c) + 1):
		if compare(c, a, i):
			ans = min(ans, len(compare(c, d, i)))

for d in bc:
	for i in range(len(d) + 1):
		if compare(d, a, i):
			ans = min(ans, len(compare(d, a, i)))

	for i in range(len(a) + 1):
		if compare(a, d, i):
			ans = min(ans, len(compare(a, d, i)))

print(ans)

Submission status: Runtime Error

Solution code:
#import sys
#input = sys.stdin.readline
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:
                    W = X[:i]
                    for j in range(i+y):
                        if X[j] == '?' or Y[j-i] == '?':
                            W += '?'
                        else:
                            W += X[j]
                    W += X[i+y:]
                    now -= y
                    y = x
                else:
                    W = X[:i]
                    for j in range(i,x):
                        if X[j] == '?' or Y[j-i] == '?':
                            W += '?'
                        else:
                            W += X[j]
                    W += Y[x-i:]
                    now -= x-i
                    y = now-z
                break
        else:
            W = X+Y
            y += x
        for i in range(y):
            check = True
            for j in range(i, min(y,i+z)):
                if W[j] == '?' or Z[j-i] == '?' or W[j] == Z[j-i]:
                    continue
                check = False
                break
            if check:
                if i+z <= y:
                    now -= z
                else:
                    now -= y-i
                break
#        print(X, Y, W, Z, y)
        if now < ans:
            ans = now
    print(ans)
        
if __name__ == '__main__':
    main()

Submission status: Runtime Error

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
                    W = X[:i]
                    for j in range(i+y):
                        if X[j] == '?' or Y[j-i] == '?':
                            W += '?'
                        else:
                            W += X[j]
                    W += X[i+y:]
                else:
                    now -= x-i
                    W = X[:i]
                    for j in range(i,x):
                        if X[j] == '?' or Y[j-i] == '?':
                            W += '?'
                        else:
                            W += X[j]
                    W += Y[x-i:]
                    y = now-z
                break
        else:
            W = X+Y
        for i in range(y):
            check = True
            for j in range(i, min(y,i+z)):
                if W[j] == '?' or Z[j-i] == '?' or W[j] == Z[j-i]:
                    continue
                check = False
                break
            if check:
                if i+z <= y:
                    now -= z
                else:
                    now -= y-i
                break
#        print(X, Y, W, Z)
        if now < ans:
            ans = now
    print(ans)
        
if __name__ == '__main__':
    main()

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]=="?" or x[i]=="?":
            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]=="?" or y[i]=="?":
            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:
#!/usr/bin/env python


def issame(s1, s2):
    for i in range(len(s1)):
        if s1[i] == s2[i] or '?' in (s1[i], s2[i]):
            continue
        return False
    return True


def same_len(s1, s2):
    n = min(len(a), len(b))
    for l in range(n):
        ll = n - l
        if issame(s1[l:], s2[:ll]):
            return ll
    else:
        return 0


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

cut = [
    same_len(a, b) + same_len(b, c),
    same_len(a, c) + same_len(c, b),
    same_len(b, a) + same_len(a, c),
    same_len(b, c) + same_len(c, a),
    same_len(c, a) + same_len(a, b),
    same_len(c, b) + same_len(b, a),
]

print(len(a) + len(b) + len(c) - max(cut))

Submission status: Runtime Error

Solution code:
import copy
#Z[i]:length of the longest list starting from S[i] which is also a prefix of S
#O(|S|)
def Z_algorithm(s,L):
    N = len(s)
    Z_alg = [0]*N

    Z_alg[0] = N
    i = L
    j = 0
    while i < N:
        while i+j < N and (s[j] == s[i+j] or s[j] == "?" or s[i+j] == "?"):
            j += 1
        Z_alg[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while i+k < N and k + Z_alg[k]<j:
            Z_alg[i+k] = Z_alg[k]
            k += 1
        i += k
        j -= k
    return Z_alg

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

def unite(x,y):
    X,Y = len(x),len(y)
    z = Z_algorithm(y+x,Y)

    tmp = -1
    for i in range(Y,X+Y):
        if z[i]+i == X+Y or z[i] == Y:
            tmp = i
            break


    if tmp == -1:
        return x+y
    else:
        if z[tmp] == Y:
            res = x
            for i in range(tmp-Y,tmp):
                if res[i+Y-tmp] == "?":
                    res[i] = y[i-(tmp-Y)]
            return res
        else:
            res = x
            for i in range(tmp-Y,X):
                if res[i] == "?":
                    res[i] = y[i-(tmp-Y)]
            for i in range(X-tmp+Y,Y):
                res.append(y[i])
            return res

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