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: Wrong Answer)

#include <stdio.h>
int da, db, dc, dx, dy, dz, ans, g, f, xi, h;
char a[2020],x[2020];
char b[2020],y[2020];
char c[2020],z[2020];
char s[7000];
int main() {
	scanf(" %s", a);
	scanf(" %s", b);
	scanf(" %s", c);
	ans = 6000;
	while (a[da] != NULL)da++;
	while (b[db] != NULL)db++;
	while (c[dc] != NULL)dc++;
	for (int e = 0; e < 6; e++) {
		if (e == 0) {
			dx = da; dy = db; dz = dc;
			for (int i = 0; i < da; i++) x[i] = a[i];
			for (int i = 0; i < db; i++) y[i] = b[i];
			for (int i = 0; i < dc; i++) z[i] = c[i];
		}
		if (e == 1) {
			dx = da; dy = dc; dz = db;
			for (int i = 0; i < da; i++) x[i] = a[i];
			for (int i = 0; i < dc; i++) y[i] = c[i];
			for (int i = 0; i < db; i++) z[i] = b[i];
		}
		if (e == 2) {
			dx = db; dy = da; dz = dc;
			for (int i = 0; i < db; i++) x[i] = b[i];
			for (int i = 0; i < da; i++) y[i] = a[i];
			for (int i = 0; i < dc; i++) z[i] = c[i];
		}
		if (e == 3) {
			dx = db; dy = dc; dz = da;
			for (int i = 0; i < db; i++) x[i] = b[i];
			for (int i = 0; i < dc; i++) y[i] = c[i];
			for (int i = 0; i < da; i++) z[i] = a[i];
		}
		if (e == 4) {
			dx = dc; dy = da; dz = db;
			for (int i = 0; i < dc; i++) x[i] = c[i];
			for (int i = 0; i < da; i++) y[i] = a[i];
			for (int i = 0; i < db; i++) z[i] = b[i];
		}
		if (e == 5) {
			dx = dc; dy = db; dz = da;
			for (int i = 0; i < dc; i++) x[i] = c[i];
			for (int i = 0; i < db; i++) y[i] = b[i];
			for (int i = 0; i < da; i++) z[i] = a[i];
		}
		for (int i = 0; i < 6001; i++)s[i] = 0;
		for (int i = 0; i < dx; i++) {
			s[i] = x[i];
		}
		g = 0;
		A:
		while (true) {
			f = 0;
			for (int i = 0; i < dy; i++) {
				if (y[i] != s[g + i] && 'a' <= s[g + i] && s[g + i] <= 'z' && 'a' <= y[i] && y[i] <= 'z') {
					f = 1;
					break;
				}
			}
			if (f == 0)break;
			g++;
		}
		h = dy + g;
		for (int i = 0; i < dy; i++) {
			if (('a' <= y[i] && y[i] <= 'z') || s[g + i] == 0) {
				s[g + i] = y[i];
			}
		}
		g = 0;
		while (true) {
			f = 0;
			for (int i = 0; i < dz; i++) {
				if (z[i] != s[g + i] && 'a' <= s[g + i] && s[g + i] <= 'z' && 'a' <= z[i] && z[i] <= 'z') {
					f = 1;
					break;
				}
			}
			if (f == 0)break;
			g++;
		}
		for (int i = 0; i < dz; i++)s[g + i] = z[i];
		xi = 0;
		while (s[xi] != 0)xi++;
		if (ans > xi) {
			ans = xi;
		}
		if (h < dx) {
			for (int i = 0; i < 6001; i++)s[i] = 0;
			for (int i = 0; i < dx; i++)s[i] = x[i];
			g = h - dy + 1;
			goto A;
		}
	}
	printf("%d\n", ans);
}