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(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(dp[ind1][ind2]!=1e9)
        return dp[ind1][ind2];
    
    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: 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;
}