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>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
using namespace std;

typedef long long ll;
// const int INF = 1e8;
const ll INF = 1LL << 60;
typedef pair<int, string> P;

const int MX = 2005;
vector<string> s;
bool dp[3][3][MX];

int main() {
    s.resize(3);

    rep(i, 3) {
        cin >> s[i];
    }

    sort(s.begin(), s.end());
    int ans = MX * 3;
    do {
        // 前処理
        rep(i, 3)rep(j, 3)rep(k, s[i].size()) {
            bool ok = true;
            for (int ni = k; ni < s[i].size(); ni++) {
                int nj = ni - k;
                if (nj >= s[j].size()) break;
                if (s[i][ni] == '?' || s[j][nj] == '?') continue;
                if (s[i][ni] != s[j][nj]) ok = false;;
            }
            dp[i][j][k] = ok;
        }

        auto f = [&](int i, int j, int k) {
            if (k >= s[i].size()) return true;
            return dp[i][j][k];
        };
        rep(x, MX)rep(y, MX) {
            if (!f(0, 1, x)) continue; 
            if (!f(1, 2, y)) continue; 
            if (!f(0, 2, x + y)) continue; 

            int now = 0;
            now = max<int>(now, x + s[0].size());
            now = max<int>(now, y + s[1].size());
            now = max<int>(now, x + y + s[2].size());

            ans = min(ans, now);
        }
    } while (next_permutation(s.begin(), s.end()));

    cout << ans << endl;
}