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: Accepted)

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 10000000000000000

bool unite(string s1,string s2,int k){
	bool f = true;

	for(int i=k;i<s1.size();i++){
		int ind1 = i;
		int ind2 = i-k;
		if(ind2>=s2.size())break;
		if(s1[ind1]!=s2[ind2]){
			if(s1[ind1]!='?'&&s2[ind2]!='?'){
				f=false;
				break;
			}
		}		
	}
	return f;
}

int main(){
	
	vector<string> s(3);
	for(int i=0;i<3;i++)cin>>s[i];
	
	vector<int> a = {0,1,2};
	
	vector<vector<vector<bool>>> B(3,vector<vector<bool>>(3,vector<bool>(4005)));
	
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			for(int k=0;k<4005;k++){
				if(i==j)continue;
				B[i][j][k] = unite(s[i],s[j],k);
				//if(B[i][j][k])cout<<i<<','<<j<<','<<k<<endl;
			}
		}
	}
	
	int X = s[0].size()+s[1].size()+s[2].size();
	
	int ans = 10000000;
	do{
		for(int i=0;i<2005;i++){
			if(!B[a[0]][a[1]][i])continue;
			for(int j=i;j<4005;j++){
				if(!B[a[0]][a[2]][j])continue;
				if(!B[a[1]][a[2]][j-i])continue;
				ans = min(ans,max({(int)s[a[0]].size(),i+(int)s[a[1]].size(),j+(int)s[a[2]].size()}));;
			}
		}
	}
	while(next_permutation(a.begin(),a.end()));
	
	cout<<ans<<endl;
	
	return 0;
}