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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<stdio.h>
#include<string.h>
int min(int a, int b)
{
	if (a < b)
		return a;
	else
		return b;
}
int f(char s[], char t[], char r[])
{
	int ss, tt, rr;
	ss = strlen(s);
	tt = strlen(t);
	rr = strlen(r);
	int i, j, k;
	char a[100005];
	for (i = 0; i < 100005; i++)
		a[i] = '\0';
	for (i = 0; i < ss; i++)
		a[i] = s[i];
	for (i = 0; i <= ss; i++)
	{
		k = 0;
		for (j = 0; j < min(tt, ss - i); j++)
		{
			if (s[i + j] != '?' && t[j] != '?' && s[i + j] != t[j])
			{
				k++;
				break;
			}
		}
		if (k == 0)
			break;
	}
	for (j = i; j < min(ss, i + tt); j++)
		if (a[j] == '?')
			a[j] = t[j - i];
	if (i + tt > ss)
	{
		for (j = ss; j < i + tt; j++)
			a[j] = t[j - i];
	}
	int aa = strlen(a);
	for (i = 0; i <= aa; i++)
	{
		k = 0;
		for (j = 0; j < min(rr, aa - i); j++)
		{
			if (a[i + j] != '?' && r[j] != '?' && a[i + j] != r[j])
			{
				k++;
				break;
			}
		}
		if (k == 0)
			break;
	}
	if (aa < i + rr)
		return i + rr;
	else
		return aa;
}
int main()
{
	char a[2003], b[2003], c[2003];
	scanf("%s%s%s", a, b, c);
	int ans = 10004;
	ans = min(ans, f(a, b, c));
	ans = min(ans, f(a, c, b));
	ans = min(ans, f(b, a, c));
	ans = min(ans, f(b, c, a));
	ans = min(ans, f(c, a, b));
	ans = min(ans, f(c, b, a));
	printf("%d\n", ans);
	return 0;
}