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 <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <queue>
#include <stack>
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
typedef long long ll;
typedef pair<int, int>  P;
typedef pair<int, P>  PP;
const int maxn=43+10;
char a[maxn],b[maxn],c[maxn];
bool xab[maxn],xba[maxn],xbc[maxn],xac[maxn],xcb[maxn];
int main()
{
    cin>>a>>b>>c;
    int lena=strlen(a),lenb=strlen(b),lenc=strlen(c);
    int len=max(max(strlen(a),strlen(b)),strlen(c));
    for(int i=-len;i<=len;i++)
    {
        int flag=0;
        for(int j=0;j<lena;j++)
        {
            if(0<=j+i&&j+i<lenb)
            {
                if(a[j]!='?'&&a[j]!=b[i+j]&&b[i+j]!='?')
                {
                    flag=1;
                    break;
                }
            }
        }
        if(flag==0){
            xab[i+len]=1;
        }
    }
    
    for(int i=-len;i<=len;i++)
    {
        int flag=0;
        for(int j=0;j<lenb;j++)
        {
            if(0<=j+i&&j+i<lenc)
            {
                if(b[j]!='?'&&b[j]!=c[i+j]&&c[i+j]!='?')
                {
                    flag=1;
                    break;
                }
            }
        }
        if(flag==0){
            xbc[i+len]=1;
        }
    }
    
    for(int i=-len;i<=len;i++)
    {
        int flag=0;
        for(int j=0;j<lena;j++)
        {
            if(0<=j+i&&j+i<lenc)
            {
                if(a[j]!='?'&&a[j]!=c[i+j]&&c[i+j]!='?')
                {
                    flag=1;
                    break;
                }
            }
        }
        if(flag==0)
            xac[i+len]=1;
    }
    
    int ans=1e9;
    for(int i=-len*2;i<=len*2;i++)
        for(int j=-len*2;j<=len*2;j++)
        {
            int l=0,r=lena;
            l=min(min(-i,-j),l);
            r=max(r,max(-i+lenb,-j+lenc));
            if(-len<=i&&i<=len&&xab[i+len]==0)
                continue;
            int p=j-i;
            if(-len<=p&&p<=len&&xbc[p+len]==0)
                continue;
            
            if(-len<=j&&j<=len&&xac[j+len]==0)
                continue;
            
            ans=min(r-l,ans);
            if(ans==4)
                cout<<i<<" "<<j<<endl;
        }
    cout<<ans;
}