Problem: 2050E - Three Strings
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Time limit exceeded on test 5):
#include<bits/stdc++.h>
using namespace std;

#define ll long long int

ll power(ll a, ll b) { if (b == 0) { return 1; } ll res = power(a, b/2); if (b%2) { return res*res*a; } else { return res*res; } }

ll lcm (ll a, ll b) { return ((a*b)/(__gcd(a,b))); }

ll cnt = 0;

ll func(string a, string b, string c, vector<vector<ll> > &dp, ll indexa, ll indexb, ll indexc, ll n1, ll n2 , ll n3) {
    cnt++;
    
    ll r1 = 1e9;
    ll r2 = 1e9;
    
    if (indexc == n3) {
        return 0;
    }
    if (dp[indexa][indexb] != -1) {
        return dp[indexa][indexb];
    }
    if (indexa == n1) {
        if (b[indexb] == c[indexc] && indexb<n2) {
            return dp[indexa][indexb] = func(a,b,c,dp,indexa,indexb+1, indexc+1, n1, n2,n3);
        }
        else if (b[indexb] != c[indexc] && indexb<n2) {
            return dp[indexa][indexb] = 1+func(a,b,c,dp,indexa,indexb+1, indexc+1, n1, n2,n3);
        }
        else {
            return 0;
        }
    }

    if (indexb == n2) {
        if (a[indexa] == c[indexc] && indexa<n1) {
            return dp[indexa][indexb] = func(a,b,c,dp,indexa+1,indexb, indexc+1, n1, n2,n3);
        }
        else if (a[indexa] != c[indexc] && indexa<n1) {
            return dp[indexa][indexb] = 1+func(a,b,c,dp,indexa+1,indexb, indexc+1, n1, n2,n3);
        }
        else {
            return 0;
        }
    }

    

    if (a[indexa] != c[indexc] && b[indexb] != c[indexc]) {
        r1 = 1 + func(a,b,c,dp,indexa+1, indexb, indexc+1, n1, n2,n3);
        r2 = 1 + func(a,b,c,dp,indexa,indexb+1, indexc+1, n1, n2,n3);
    }
    else {
        if (a[indexa] == c[indexc]) {
        r1 = func(a,b,c,dp,indexa+1, indexb, indexc+1, n1, n2,n3);
        }
        else {
            r1 = 1 + func(a,b,c,dp,indexa+1, indexb,indexc+1, n1, n2,n3);
        }

        if (b[indexb] == c[indexc]) {
        r2 = func(a,b,c,dp,indexa,indexb+1, indexc+1, n1, n2,n3);
        }
        else {
            r2 = 1+func(a,b,c,dp,indexa, indexb+1, indexc+1, n1, n2,n3);
        }        
    }
    return dp[indexa][indexb] = min(r1,r2);
}

int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    ll t;
    cin >> t;

    while(t--) {
        string a,b,c;
        cin >> a >> b >> c;
        vector<vector<ll> > dp(1001, vector<ll> (1001, -1));
        ll n1 = a.length();
        ll n2 = b.length();
        ll n3 = c.length();
        ll ans = func(a,b,c,dp,0LL,0LL,0LL, n1 , n2 , n3);
        cout << ans << endl;
    }
    //cout << cnt << endl;
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;

#define ll long long int

ll power(ll a, ll b) { if (b == 0) { return 1; } ll res = power(a, b/2); if (b%2) { return res*res*a; } else { return res*res; } }

ll lcm (ll a, ll b) { return ((a*b)/(__gcd(a,b))); }

ll cnt = 0;

ll func(string &a, string &b, string &c, vector<vector<ll> > &dp, ll indexa, ll indexb, ll indexc, ll &n1, ll &n2, ll &n3) {
    cnt++;
    
    ll r1 = 1e9;
    ll r2 = 1e9;

    if (indexc == n3) {
        return 0;
    }

    // if (indexa == n1) {
    //     if (b[indexb] == c[indexc] && indexb<n2) {
    //         return dp[indexa][indexb] = func(a,b,c,dp,indexa,indexb+1, indexc+1);
    //     }
    //     else if (b[indexb] != c[indexc] && indexb<n2) {
    //         return dp[indexa][indexb] = 1+func(a,b,c,dp,indexa,indexb+1, indexc+1);
    //     }
    //     else {
    //         return 0;
    //     }
    // }

    // if (indexb == n2) {
    //     if (a[indexa] == c[indexc] && indexa<n1) {
    //         return dp[indexa][indexb] = func(a,b,c,dp,indexa+1,indexb, indexc+1);
    //     }
    //     else if (a[indexa] != c[indexc] && indexa<n1) {
    //         return dp[indexa][indexb] = 1+func(a,b,c,dp,indexa+1,indexb, indexc+1);
    //     }
    //     else {
    //         return 0;
    //     }
    // }

    if (dp[indexa][indexb] != -1) {
        return dp[indexa][indexb];
    }

    if (indexa < n1 && a[indexa] == c[indexc]) {
        r1 = func(a,b,c,dp,indexa+1,indexb,indexc+1,n1,n2,n3);
    }
    if (indexa < n1 && a[indexa] != c[indexc]) {
        r1 = min(r1, 1 + func(a,b,c,dp,indexa+1,indexb,indexc+1,n1,n2,n3));
    }
    if (indexb < n2 && b[indexb] == c[indexc]) {
        r1 = min(r1, func(a,b,c,dp,indexa,indexb+1,indexc+1,n1,n2,n3));
    }
    if (indexb < n2 && b[indexb] != c[indexc]) {
        r1 = min(r1, 1 + func(a,b,c,dp,indexa,indexb+1,indexc+1,n1,n2,n3));
    }
    return dp[indexa][indexb] = min(r1,r2);
}

int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);


    ll t;
    cin >> t;

    while(t--) {
        string a,b,c;
        cin >> a >> b >> c;
        ll n1 = a.length();
        ll n2 = b.length();
        ll n3 = c.length();
        vector<vector<ll> > dp(n1+1, vector<ll> (n2+1, -1));
        
        ll ans = func(a,b,c,dp,0LL,0LL,0LL, n1, n2, n3);
        cout << ans << endl;
    }
    // cout << cnt << endl;
    // cerr << endl << "Execution Took: " << 1000 * clock() / CLOCKS_PER_SEC << " ms." << endl;
}