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)

#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define pi  acos(-1)
const int w=5e5+5;
const ll mod=1e9+7;
char a[5][2005];
int f[5];
int len[5];
int main()
{
    int i,j,k;
    scanf("%s",a[1]+1);
    scanf("%s",a[2]+1);
    scanf("%s",a[3]+1);
    len[1]=strlen(a[1]+1),len[2]=strlen(a[2]+1),len[3]=strlen(a[3]+1);
    int ans=len[1]+len[2]+len[3];
    int v=ans;
    for(i=1;i<=3;i++)
        f[i]=i;
    do
    {
        int u=v;
        for(i=1;i<=len[f[2]];i++)
            a[4][i]=a[f[2]][i];
        for(i=min(len[f[1]],len[f[2]]);i>=1;i--)
        {
            int flag=0;
            for(j=len[f[1]]-i+1,k=1;k<=i;j++,k++)
            {
                //cout<<a[f[1]][j]<<" "<<a[f[2]][k]<<endl;
                if(a[f[1]][j]==a[f[2]][k]||a[f[1]][j]=='?'||a[f[2]][k]=='?')
                    continue;
                else
                    flag=1;
            }
            if(flag==0)
            {
                u-=i;
                for(j=len[f[1]]-i+1,k=1;k<=i;j++,k++)
                {
                    if(a[f[1]][j]==a[f[2]][k])
                        continue;
                    else
                    {
                        if(a[f[2]][k]=='?')
                            a[4][k]=a[f[1]][j];
                    }
                }
                break;
            }
        }
        for(i=min(len[f[2]],len[f[3]]);i>=1;i--)
        {
            int flag=0;
            for(j=len[f[2]]-i+1,k=1;k<=i;j++,k++)
            {
                if(a[4][j]==a[f[3]][k]||a[4][j]=='?'||a[f[3]][k]=='?')
                    continue;
                else
                    flag=1;
            }
            if(flag==0)
            {
                u-=i;
                break;
            }
        }
        ans=min(ans,u);
    }
    while(next_permutation(f+1,f+4));
    cout<<ans<<endl;
    return 0;
}
/*
aa
bb
??
*/