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;

using ll = long long int;
using P = pair<ll, ll>;
using P3 = pair<double,P>;
using PP = pair<P, P>;
constexpr int INF = 1 << 30;
constexpr ll MOD = ll(1e9+7);
constexpr int di[] = {0, 1, 0, -1};
constexpr int dj[] = {1, 0, -1, 0};
constexpr double EPS = 1e-9;

int pos[3][3];

int solve(string& s, string& t){
    int n = s.size(), m = t.size();
    for(int i=0;i<n;i++){
        bool ok = true;
        for(int j=0;i+j<n&&j<m;j++){
            ok &= s[i+j]==t[j] || s[i+j]=='?' || t[j]=='?';
        }
        if(ok){
            return i;
        }
    }
    return n;
}

int solve2(string& s, string& t, string& u, int x){
    string a = s;
    int n = s.size(), m = t.size(), l = u.size();
    for(int i=0;i<m&&i+x<n;i++){
        if(a[i+x]=='?') a[i+x] = t[i];
    }
    if(m > n-x) a += t.substr(n-x, m-(n-x));
    return max(solve(a,u)+(int)u.size(), (int)a.size());
}

int main(){
    string s[3];
    for(int i=0;i<3;i++){
        cin >> s[i];
    }
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            pos[i][j] = INF;
            if(i==j) continue;
            pos[i][j] = solve(s[i], s[j]);
        }
    }
    
    int ans = INF;
    vector<int> p = {0,1,2};
    while(1){
        ans = min(ans, solve2(s[p[0]],s[p[1]],s[p[2]],pos[p[0]][p[1]]));
        if(!next_permutation(p.begin(),p.end())) break;
    }
    cout << ans << endl;
    return 0;
}