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 9.2.1) (judge verdict: Accepted)

///Bismillahir Rahmanir Rahim
#include "bits/stdc++.h"

#define                           fi                          first
#define                           si                          second
#define                           mp                          make_pair
#define                           pb                          push_back
#define                           pi                          pair<ll,ll>
#define                           clr(x)                      memset(x,0,sizeof(x));
#define                           f(i,l,r)                    for(int i=l;i<=r;i++)
#define                           rf(i,r,l)                   for(int i=r;i>=l;i--)
#define                           done(i)                     cout<<"done = "<<i<<endl;
#define                           show(x,y)                   cout<<x<<" : ";for(auto z:y)cout<<z<<" ";cout<<endl;
#define                           fast                        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

const int mod=1e9+7;
const int M=10100;
string a,b,c,tmp="";
int asz,bsz,csz;
 int dp1[M][2007];
 int dp2[M][2007];
 int dp3[M][2007];
bool chk(int bl,int br,int cl,int cr)
{
    if(br<cl || cr<bl)return true;
    int lft=max(bl,cl);
    int rgt=min(br,cr);
    int len=rgt-lft+1;
    int bst=lft-bl;
    int cst=lft-cl;
    if(dp3[bst][cst]>=len)return true;
    return false;
}
 main()

{
    //fast
    //freopen("005.txt", "r", stdin);
    cin>>a;
    cin>>b;
    cin>>c;
//    cout<<"#"<<a<<endl;
//    cout<<"#"<<b<<endl;
//    cout<<"#"<<c<<endl;
    int ctmp=4002;
    while(ctmp--)tmp+="?";
    int alen=a.size();
    a=a+tmp;
    a=tmp+a;
    asz=a.size();
    bsz=b.size();
    csz=c.size();
   // cout<<alen<<" "<<bsz<<" "<<csz<<endl;
    int al=4002;
    int ar=al+alen-1;
    rf(i,asz-1,0)
    {
        rf(j,bsz-1,0)
        {
            if(a[i]==b[j] || a[i]=='?' ||  b[j]=='?')
            {
                dp1[i][j]=1+dp1[i+1][j+1];
            }
        }
    }
    rf(i,asz-1,0)
    {
        rf(j,csz-1,0)
        {
            if(a[i]==c[j] || a[i]=='?' ||  c[j]=='?')
            {
                dp2[i][j]=1+dp2[i+1][j+1];
            }
        }
    }
    rf(i,bsz-1,0)
    {
        rf(j,csz-1,0)
        {
            if(b[i]==c[j] || b[i]=='?' || c[j]=='?')
            {
                dp3[i][j]=1+dp3[i+1][j+1];
            }
        }
    }
    int ses=alen+bsz+csz;
   f(bst,0,asz-1)
   {
       f(cst,0,asz-1)
       {
           if(dp1[bst][0]==bsz && dp2[cst][0]==csz)
           {
               int bl=bst;
               int br=bst+bsz-1;
               int cl=cst;
               int cr=cst+csz-1;
               if(chk(bl,br,cl,cr)){

               int lft=min(al,bl);
               lft=min(lft,cl);
               int rgt=max(ar,br);
               rgt=max(rgt,cr);
               int sz=rgt-lft+1;
               ses=min(ses,sz);

               }
           }
       }
   }
   cout<<ses<<endl;
    return 0;

}