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.

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include<bits/stdc++.h>

using namespace std;

#define FOR(i, x, y) for(int i = (x); i < (y); ++i)
#define REP(i, x, y) for(int i = (x); i <= (y); ++i)
#define MP make_pair
#define PB push_back
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;

const int INF = 1e9 + 7;
const int maxn = 2005;

int ans = INF;
int ab[maxn][maxn], ac[maxn][maxn], bc[maxn][maxn];
string a, b, c;

inline bool match(int mat[][maxn], int xl, int xr, int yl, int yr){
	int zl = max(xl, yl), zr = min(xr, yr);
	return mat[zl - xl][zl - yl] >= (zr - zl + 1);
}

int main(){
	cin >> a >> b >> c;
	for(int i = a.size() - 1; i >= 0; --i) for(int j = b.size() - 1; j >= 0; --j)
		ab[i][j] = (a[i] != b[j] && a[i] != '?' && b[j] != '?' ? 0 : ab[i + 1][j + 1] + 1);
	for(int i = a.size() - 1; i >= 0; --i) for(int j = c.size() - 1; j >= 0; --j)
		ac[i][j] = (a[i] != c[j] && a[i] != '?' && c[j] != '?' ? 0 : ac[i + 1][j + 1] + 1);
	for(int i = b.size() - 1; i >= 0; --i) for(int j = c.size() - 1; j >= 0; --j)
		bc[i][j] = (b[i] != c[j] && b[i] != '?' && c[j] != '?' ? 0 : bc[i + 1][j + 1] + 1);
	int tot = a.size() + b.size() + c.size();
	FOR(i, 0, tot) FOR(j, 0, tot){
		int al = i, ar = i + a.size() - 1,
			bl = j, br = j + b.size() - 1,
			cl = 0, cr = 0 + c.size() - 1;
		if(!match(ab, al, ar, bl, br))
			continue;
		if(!match(ac, al, ar, cl, cr))
			continue;
		if(!match(bc, bl, br, cl, cr))
			continue;
		ans = min(ans, max(ar, max(br, cr)) + 1);
	}
	FOR(i, 0, tot) FOR(j, 0, tot){
		int al = i, ar = i + a.size() - 1,
			bl = 0, br = 0 + b.size() - 1,
			cl = j, cr = j + c.size() - 1;
		if(!match(ab, al, ar, bl, br))
			continue;
		if(!match(ac, al, ar, cl, cr))
			continue;
		if(!match(bc, bl, br, cl, cr))
			continue;
		ans = min(ans, max(ar, max(br, cr)) + 1);
	}
	FOR(i, 0, tot) FOR(j, 0, tot){
		int al = 0, ar = 0 + a.size() - 1,
			bl = i, br = i + b.size() - 1,
			cl = j, cr = j + c.size() - 1;
		if(!match(ab, al, ar, bl, br))
			continue;
		if(!match(ac, al, ar, cl, cr))
			continue;
		if(!match(bc, bl, br, cl, cr))
			continue;
		ans = min(ans, max(ar, max(br, cr)) + 1);
	}
	printf("%d\n", ans);
	return 0;
}