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)

//Utkarsh.25dec
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define rep(i,n) for(ll i=0;i<n;i++)
#define loop(i,a,b) for(ll i=a;i<=b;i++)
#define vi vector <int>
#define vs vector <string>
#define vc vector <char>
#define vl vector <ll>
#define all(c) (c).begin(),(c).end()
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
using namespace std;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll modInverse(ll a){return power(a,mod-2);}
const int N=500023;
bool vis[N];
vector <int> adj[N];
int check(string a,string b,string c)
{
    ll mini=INT_MAX;
    for(int i=0;i<2002;i++)
    {
        string s="";
        s=a;
        if((i+c.length())<(b.length()))
            continue;
        if((i+c.length())<(a.length()))
            continue;
        while(s.length()<(i+c.length()))
            s.pb('?');
        int flag=1;
        for(int j=i;j<i+c.length();j++)
        {
            if((s[j]!='?') && (c[j-i]!='?'))
            {
                if(s[j]!=c[j-i])
                {
                    flag=0;
                    break;
                }
            }
            else
            {
                if(s[j]=='?')
                    s[j]=c[j-i];
            }
        }
        if(flag==1)
        {
            for(int k=0;k<i+c.length();k++)
            {
                if((b.length()+k)>(i+c.length()))
                    break;
                int flag2=1;
                for(int j=k;j<k+b.length();j++)
                {
                    if(s[j]=='?')
                        continue;
                    if(b[j-k]=='?')
                        continue;
                    if(s[j]!=b[j-k])
                    {
                        flag2=0;
                        break;
                    }
                }
                if(flag2==1)
                {
                    mini=min(mini,i+(ll)c.length());
                    return mini;
                }
            }
        }
    }
    return mini;
}
void solve()
{
    string a,b,c;
    cin>>a>>b>>c;
    int ans=check(a,b,c);
    ans=min(ans,check(a,c,b));
    ans=min(ans,check(b,c,a));
    ans=min(ans,check(b,a,c));
    ans=min(ans,check(c,a,b));
    ans=min(ans,check(c,b,a));
    cout<<ans;
    // int flag1=min((int)check(a,b,c),(int)check(a,c,b),(int)check(b,a,c));
    // int flag2=min(check(b,c,a),check(c,a,b),check(c,b,a));
    // cout<<min(flag1,flag2);
    // while(l<=r)
    // {
    //     ll mid=(l+r)/2;
    //     int flag1=max3(check(a,b,c,mid),check(a,c,b,mid),check(b,a,c,mid));
    //     int flag2=max3(check(b,c,a,mid),check(c,a,b,mid),check(c,b,a,mid));
    //     if(max(flag1,flag2)==1)
    //     {
    //         r=mid-1;
    //     }
    //     else
    //         l=mid+1;
    // }
    // cout<<l;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int T=1;
    //cin>>T;
    int t=0;
    while(t++<T)
    {
        //cout<<"Case #"<<t<<":"<<' ';
        solve();
        //cout<<'\n';
    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}