Language: C++

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define REP(i, a, b) for(int i = a; i <= b; ++i)
#define DEC(i, a, b) for(int i = a; i >= b; --i)
#define getmid(l,r) ((l) + ((r) - (l)) / 2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define FI first
#define SE second
#define SZ(x) ((int)(x).size())
#define MP make_pair
#define PB push_back
 
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<pii > VPI;
typedef vector<pll > VPL;
const int mod = 1000000007;

char S[3][2010];
int avai[3][3][2010], len[3];
int clu[3][3], lmin[3][3];

bool check(int a, int b, int L){
    REP(i, 1, L){
        if(S[b][L - i + 1] != '?' && S[a][len[a] - i + 1] != '?'
                && S[b][L - i + 1] != S[a][len[a] - i + 1]) return false;
    }
    return true;
}

bool judge(int a, int b, int k){
    REP(i, k, k + len[b] - 1){
        if(S[a][i] != '?' && S[b][i - k + 1] != '?' &&
                S[a][i] != S[b][i - k + 1]) return false;
    }
    return true;
}

int Cal(int a, int b, int c){
    int res = 1 << 30;
    if(clu[a][b]){
        if(clu[a][c]) res = len[a];
        else res = len[a] + lmin[a][c];
    }
    else{
        if(clu[b][c]) res = len[a] + lmin[a][b];
        else{
            res = min(res, len[a] + len[b] + lmin[b][c]);
            res = min(res, len[a] + lmin[a][b] + len[c]);
            REP(i, 1, len[b]) if(avai[a][b][i]){
                REP(j, 1, len[c]) if(avai[b][c][j]){
                    int X = j - (len[b] - i);
                    if(X > 0 && avai[a][c][X] == 0) continue;
                    res = min(res, len[a] + len[b] - i + len[c] - j);
                }
            }
        }
    }
    return res;
}

int main(){
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> S[0] + 1 >> S[1] + 1 >> S[2] + 1;
    REP(i, 0, 2) len[i] = strlen(S[i] + 1);
    REP(i, 0, 2) REP(j, 0, 2) lmin[i][j] = len[j];
    REP(i, 0, 2){
        REP(j, 0, 2) if(i != j){
            REP(k, 1, min(len[i], len[j])){
                if(check(i, j, k)){
                    avai[i][j][k] = 1;
                    lmin[i][j] = min(lmin[i][j], len[j] - k);
                }
            }
            if(len[i] >= len[j]){
                REP(k, 1, len[i] - len[j] + 1) if(judge(i, j, k)){
                    clu[i][j] = true;
                    break;
                }
            }
        }
    }
    int ans = len[0] + len[1] + len[2];
    do{
        ans = min(ans, cal(arr[0], arr[1], arr[2]));
    }while(next_permutation(arr, arr + 3));
    cout << ans << endl;
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:91:28: error: 'arr' was not declared in this scope
   91 |         ans = min(ans, cal(arr[0], arr[1], arr[2]));
      |                            ^~~
a.cc:91:24: error: 'cal' was not declared in this scope; did you mean 'Cal'?
   91 |         ans = min(ans, cal(arr[0], arr[1], arr[2]));
      |                        ^~~
      |                        Cal
a.cc:92:29: error: 'arr' was not declared in this scope
   92 |     }while(next_permutation(arr, arr + 3));
      |                             ^~~