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

Previous submission (verdict: Time limit exceeded on test 16):
#include <iostream>
#include <vector>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;
int minOperations(string a, string b, string c) {
    int n = a.length(), m = b.length(), l = c.length();
    vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
    vector<vector<int>> next(n + 1, vector<int>(m + 1, 0));

    // Iterate over k from l to 0
    for (int k = l; k >= 0; --k) {
        for (int i = n; i >= 0; --i) {
            for (int j = m; j >= 0; --j) {
                if (i == n && j == m) {
                    dp[i][j] = 0; // Base case
                } else if (i < n && j < m) {
                    if (a[i] == b[j]) {
                        if (k < l && a[i] == c[k]) {
                            dp[i][j] = min(next[i + 1][j], next[i][j + 1]);
                        } else {
                            dp[i][j] = 1 + min(next[i + 1][j], next[i][j + 1]);
                        }
                    } else {
                        if (k < l && a[i] == c[k]) {
                            dp[i][j] = min(next[i + 1][j], 1 + next[i][j + 1]);
                        } else if (k < l && b[j] == c[k]) {
                            dp[i][j] = min(1 + next[i + 1][j], next[i][j + 1]);
                        } else {
                            dp[i][j] = 1 + min(next[i + 1][j], next[i][j + 1]);
                        }
                    }
                } else if (i < n) {
                    if (k < l && a[i] == c[k]) {
                        dp[i][j] = next[i + 1][j];
                    } else {
                        dp[i][j] = 1 + next[i + 1][j];
                    }
                } else if (j < m) {
                    if (k < l && b[j] == c[k]) {
                        dp[i][j] = next[i][j + 1];
                    } else {
                        dp[i][j] = 1 + next[i][j + 1];
                    }
                }
            }
        }
        next = dp; // Move to the next layer
    }

    return dp[0][0];
}


int main(){
    int t;
    cin>>t;
    while(t--){
      
       string a,b,c;
       cin>>a>>b>>c;
vector<vector<int>>dp(a.length()+1,vector<int>(b.length()+1,-1));

     cout<<   minOperations(a,b,c);

   cout<<endl;


     
    }
    return 0;
}

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

int minChanges(string &a, string &b, string &c) {
    int n = a.length(), m = b.length();
    vector<int> prev(m + 1, 0), curr(m + 1, 0);

    // Initialize base cases
    for (int j = 0; j <= m; ++j) {
        prev[j] = j; // Only use characters from `b`
    }

    for (int i = 0; i <= n; ++i) {
        curr[0] = i; // Only use characters from `a`

        for (int j = 0; j <= m; ++j) {
            if (i == 0 && j == 0) continue; // Base case already handled
            int k = i + j - 1; // Current index in `c`

            curr[j] = INT_MAX; // Initialize to a large value

            if (i > 0 && a[i - 1] == c[k]) { // Match with `a`
                curr[j] = min(curr[j], prev[j]);
            } else if (i > 0) { // Mismatch with `a`
                curr[j] = min(curr[j], prev[j] + 1);
            }

            if (j > 0 && b[j - 1] == c[k]) { // Match with `b`
                curr[j] = min(curr[j], curr[j - 1]);
            } else if (j > 0) { // Mismatch with `b`
                curr[j] = min(curr[j], curr[j - 1] + 1);
            }
        }

        // Swap `prev` and `curr`
        swap(prev, curr);
    }

    return prev[m];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;
    while (t--) {
        string a, b, c;
        cin >> a >> b >> c;
        cout << minChanges(a, b, c) << '\n';
    }

    return 0;
}