Problem: 2050E - Three Strings
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 16):

#include<bits/stdc++.h>
using namespace std;
#define int long long
 
int mod = 1e9+7;

int solve(int ind1, int ind2, string a, string b, string c, int n, int m, vector<vector<int>> &dp){
    if(dp[ind1][ind2]!=1e9)
        return dp[ind1][ind2];
        
    if(ind1==n && ind2==m)
        return 0;

    if(ind1==n){
        if(c[ind1+ind2]!=b[ind2])
            return dp[ind1][ind2] = 1+solve(ind1,ind2+1,a,b,c,n,m,dp);
        else
            return dp[ind1][ind2] = solve(ind1,ind2+1,a,b,c,n,m,dp);
    }

    if(ind2==m){
        if(c[ind1+ind2]!=a[ind1])
            return dp[ind1][ind2] = 1+solve(ind1+1,ind2,a,b,c,n,m,dp);
        else
            return dp[ind1][ind2] = solve(ind1+1,ind2,a,b,c,n,m,dp);
    }
    
    if(c[ind1+ind2]==a[ind1] && c[ind1+ind2]!=b[ind2])
        return dp[ind1][ind2] = min(solve(ind1+1,ind2,a,b,c,n,m,dp),1+solve(ind1,ind2+1,a,b,c,n,m,dp));
    
    if(c[ind1+ind2]!=a[ind1] && c[ind1+ind2]==b[ind2])
        return dp[ind1][ind2] = min(1+solve(ind1+1,ind2,a,b,c,n,m,dp),solve(ind1,ind2+1,a,b,c,n,m,dp));

    if(c[ind1+ind2]==a[ind1] && c[ind1+ind2]==b[ind2])
        return dp[ind1][ind2] = min(solve(ind1+1,ind2,a,b,c,n,m,dp),solve(ind1,ind2+1,a,b,c,n,m,dp));

    if(c[ind1+ind2]!=a[ind1] && c[ind1+ind2]!=b[ind2])
        return dp[ind1][ind2] = 1+min(solve(ind1+1,ind2,a,b,c,n,m,dp),solve(ind1,ind2+1,a,b,c,n,m,dp));
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t=1;
    cin>>t;
 
    while(t--){
        string a,b,c;
        cin>>a;
        cin>>b;
        cin>>c;
        int n = a.size();
        int m = b.size();
        vector<vector<int>> dp(n+1, vector<int>(m+1,1e9));

        int ans = solve(0,0,a,b,c,n,m,dp);
        cout<<ans<<endl;
    }
    return 0;
}

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

int mod = 1e9 + 7;

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t = 1;
    cin >> t;

    while (t--) {
        string a, b, c;
        cin>>a;
        cin>>b;
        cin>>c;
        int n = a.size();
        int m = b.size();
        vector<vector<int>> dp(n+1,vector<int>(m+1, 1e9));
        
        dp[n][m] = 0; 

        for (int ind1=n;ind1>=0;ind1--){
            for (int ind2=m;ind2>=0;ind2--){
                if(ind1==n && ind2==m) continue;

                int index = ind1 + ind2;
                if(ind1==n){
                    if (c[index] != b[ind2])
                        dp[ind1][ind2] = min(dp[ind1][ind2], 1 + dp[ind1][ind2 + 1]);
                    else
                        dp[ind1][ind2] = min(dp[ind1][ind2], dp[ind1][ind2 + 1]);
                }
                else if(ind2==m){
                    if (c[index] != a[ind1])
                        dp[ind1][ind2] = min(dp[ind1][ind2], 1 + dp[ind1 + 1][ind2]);
                    else
                        dp[ind1][ind2] = min(dp[ind1][ind2], dp[ind1 + 1][ind2]);
                }
                else{
                    if (c[index] == a[ind1] && c[index] != b[ind2])
                        dp[ind1][ind2] = min(dp[ind1 + 1][ind2], 1 + dp[ind1][ind2 + 1]);
                    if (c[index] != a[ind1] && c[index] == b[ind2])
                        dp[ind1][ind2] = min(1 + dp[ind1 + 1][ind2], dp[ind1][ind2 + 1]);
                    if (c[index] == a[ind1] && c[index] == b[ind2])
                        dp[ind1][ind2] = min(dp[ind1][ind2], min(dp[ind1 + 1][ind2], dp[ind1][ind2 + 1]));
                    if (c[index] != a[ind1] && c[index] != b[ind2])
                        dp[ind1][ind2] = min(dp[ind1][ind2],1 + min(dp[ind1 + 1][ind2],dp[ind1][ind2 + 1]));
                }
            }
        }

        cout << dp[0][0] << endl;
    }

    return 0;
}