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>
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;

template<class T>
void chmax(T &a, const T b) {
    if (b > a) a = b;
}

template<class T>
void chmin(T &a, const T b) {
    if (b < a) a = b;
}

namespace Math {
    ll modpow(ll b, ll p, ll m) {
        ll r = 1;
        for (; p; p >>= 1, b = b * b % m)
            if (p & 1)
                r = r * b % m;
        return r;
    }

    ll modinv(ll b, ll m) { return modpow(b, m - 2, m); }

    // Least significant bit.
    ll lsb(ll x) { return x & (-x); }

    template<class T>
    const T inf() { return 0; }

    template<>
    const ll inf<ll>() { return 0x3f3f3f3f3f3f3f3fll; }

    template<>
    const int inf<int>() { return 0x3f3f3f3f; }

    template<class T>
    T gcd(T a, T b) { return abs(__gcd(a, b)); }

    template<class T>
    T clamp(T n, T l, T r) { return n < l ? l : n > r ? r : n; }

    template<class T>
    T lcm(T a, T b) { return abs(a / gcd(a, b) * b); }

    template<class T>
    T sgn(T n) { return n == 0 ? 0 : n > 0 ? 1 : -1; }

    // Generic modular integer.
    template<int MOD, typename IntType = int>
    struct GModint {
        IntType val;

        GModint() : val(0) {}

        GModint(int v, bool safe = false) { val = safe ? v : ((v % MOD) + MOD) % MOD; }

        GModint(ll v, bool safe = false) { val = safe ? v : ((v % MOD) + MOD) % MOD; }

        explicit operator bool() const { return val != 0; }

        const bool operator==(const GModint &g) const { return val == g.val; }

        const bool operator!=(const GModint &g) const { return val != g.val; }

        const GModint operator+=(const GModint &g) { return *this = *this + g; }

        const GModint operator-=(const GModint &g) { return *this = *this - g; }

        const GModint operator*=(const GModint &g) { return *this = *this * g; }

        const GModint operator/=(const GModint &g) { return *this = *this / g; }

        const GModint operator^=(const GModint &g) { return *this = *this ^ g; }

        const GModint operator-() const { return GModint(val == 0 ? 0 : MOD - val, true); }

        const GModint operator+(const GModint &g) const {
            ll newval = val + g.val;
            if (newval >= MOD)
                newval -= MOD;
            return GModint(newval, true);
        }

        const GModint operator-(const GModint &g) const { return *this + (-g); }

        const GModint operator*(const GModint &g) const { return GModint(1ll * val * g.val); }

        const GModint operator/(const GModint &g) const { return *this * g.inv(); }

        template<class T>
        const GModint operator^(const T t) const { return GModint(modpow(val, t, MOD)); }

        const GModint inv() const { return GModint(modinv(val, MOD)); }
    };

    using Modint = GModint<1000000007>;
}

string bind2(string p,string q){
    for (int i=0;i<p.length();i++){
        bool ok=true;
        for (int j=0;j+i<p.length()&&j<q.length();j++){
            char a=p[j+i],b=q[j];
            ok&=a=='?'||b=='?'||a==b;
        }
        if(ok){
            string s;
            if (i+q.length()<p.length()){
                s=p;
            }else{
                s=p.substr(0,i)+q;
            }
            for (int j=0;j+i<p.length()&&j<q.length();j++){
                char a=p[j+i],b=q[j];
                if (a!='?')s[i+j]=a;
                else s[i+j]=b;
            }
            return s;
        }
    }
    return p+q;
}

string bind3(string p,string q,string r){
    return bind2(bind2(p,q),bind2(q,r));
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    string s[3];
    cin>>s[0]>>s[1]>>s[2];
    int a[]={0,1,2};

    int p=s[0].length()+s[1].length()+s[2].length();

    do{
        string q=bind3(s[a[0]],s[a[1]],s[a[2]]);
        p=min(p,(int)q.length());
    }while(next_permutation(a,a+3));

    cout<<p<<endl;
}