Problem: 2050E - Three Strings
Language: c++17 

Solution 1 (runtime: 92 ms):
#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;
}


Solution 2 (runtime: 484 ms):
#include <bits/stdc++.h>
using namespace std;

// Function to solve using optimized DP
int solve(string& a, string& b, string& c) {
    int n = a.length(), m = b.length(), k = c.length();
    
    if (n + m != k) return -1; // If lengths don't match, interleaving is impossible
    
    // DP table: we use 2 rows to optimize space (current and previous state)
    vector<vector<int>> dp(m + 1, vector<int>(2, INT_MAX));
    
    dp[0][0] = 0; // Base case: no changes needed when all strings are empty
    
    // Iterate through characters in `c`
    for (int cItr = 1; cItr <= k; cItr++) {
        vector<vector<int>> newDp(m + 1, vector<int>(2, INT_MAX));
        
        for (int aItr = 0; aItr <= min(cItr, n); aItr++) {
            int bItr = cItr - aItr;
            if (bItr > m || bItr < 0) continue; // Skip invalid states
            
            // Transition from previous states
            if (aItr > 0 && c[cItr - 1] == a[aItr - 1]) {
                newDp[bItr][cItr % 2] = min(newDp[bItr][cItr % 2], dp[bItr][(cItr - 1) % 2]);
            }
            
            if (bItr > 0 && c[cItr - 1] == b[bItr - 1]) {
                newDp[bItr][cItr % 2] = min(newDp[bItr][cItr % 2], dp[bItr - 1][(cItr - 1) % 2]);
            }
            
            // Modify character `c[cItr - 1]`
            if (aItr > 0) {
                newDp[bItr][cItr % 2] = min(newDp[bItr][cItr % 2], dp[bItr][(cItr - 1) % 2] + 1);
            }
            if (bItr > 0) {
                newDp[bItr][cItr % 2] = min(newDp[bItr][cItr % 2], dp[bItr - 1][(cItr - 1) % 2] + 1);
            }
        }
        
        dp = newDp; // Update DP state
    }
    
    return dp[m][k % 2]; // Result is stored in the final state
}

int main() {
    int t;
    cin >> t;

    while (t--) {
        string a, b, c;
        cin >> a >> b >> c;

        // Solve for the minimum number of changes
        cout << solve(a, b, c) << endl;
    }

    return 0;
}