Problem: 2050E - Three Strings
Language: C++20 (GCC 13-64)

Previous submission (verdict: Memory limit exceeded on test 14):
// https://codeforces.com/problemset/problem/2050/E

#include <bits/stdc++.h>
using namespace std;

int rec(string& a, string& b, string& c, int i, int j, int k, vector<vector<vector<int>>>& dp) {
    if (k == c.size()) return 0;
    if (dp[i][j][k] != -1) return dp[i][j][k];

    int minAns = INT_MAX;

    if (i < a.size() && j < b.size()) {
        if (a[i] == c[k] && b[j] == c[k]) {
            minAns = min({rec(a, b, c, i + 1, j, k + 1, dp),
                          rec(a, b, c, i, j + 1, k + 1, dp)});
        } else if (a[i] == c[k]) {
            minAns = min(rec(a, b, c, i + 1, j, k + 1, dp), 
                         1 + rec(a, b, c, i, j + 1, k + 1, dp));
        } else if (b[j] == c[k]) {
            minAns = min(1 + rec(a, b, c, i + 1, j, k + 1, dp), 
                         rec(a, b, c, i, j + 1, k + 1, dp));
        } else {
            minAns = 1 + min(rec(a, b, c, i + 1, j, k + 1, dp), 
                             rec(a, b, c, i, j + 1, k + 1, dp));
        }
    } else if (i < a.size()) {
        int add = (a[i] != c[k]) ? 1 : 0;
        minAns = add + rec(a, b, c, i + 1, j, k + 1, dp);
    } else if (j < b.size()) {
        int add = (b[j] != c[k]) ? 1 : 0;
        minAns = add + rec(a, b, c, i, j + 1, k + 1, dp);
    }

    return dp[i][j][k] = minAns;
}

void solve() {
    string a, b, c;
    cin >> a >> b >> c;

    vector<vector<vector<int>>> dp(a.size() + 1, vector<vector<int>>(b.size() + 1, vector<int>(c.size() + 1, -1)));
    int minRep = rec(a, b, c, 0, 0, 0, dp);
    cout << minRep << endl;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


Submission (verdict: Time limit exceeded on test 16):
// https://codeforces.com/problemset/problem/2050/E

#include <bits/stdc++.h>
using namespace std;

void solve() {
    string a, b, c;
    cin >> a >> b >> c;

    int n = a.size(), m = b.size(), o = c.size();
    vector<vector<int>> prev(n + 1, vector<int>(m + 1, INT_MAX));
    vector<vector<int>> curr(n + 1, vector<int>(m + 1, INT_MAX));

    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= m; ++j) {
            prev[i][j] = 0;
        }
    }

    for (int k = o - 1; k >= 0; --k) {
        for (int i = n; i >= 0; --i) {
            for (int j = m; j >= 0; --j) {
                int minAns = INT_MAX;
                if (i < n && j < m) {
                    if (a[i] == c[k] && b[j] == c[k]) {
                        minAns = min({prev[i + 1][j], prev[i][j + 1]});
                    } else if (a[i] == c[k]) {
                        minAns = min(prev[i + 1][j], 1 + prev[i][j + 1]);
                    } else if (b[j] == c[k]) {
                        minAns = min(1 + prev[i + 1][j], prev[i][j + 1]);
                    } else {
                        minAns = 1 + min(prev[i + 1][j], prev[i][j + 1]);
                    }
                } else if (i < n) {
                    minAns = (a[i] != c[k]) + prev[i + 1][j];
                } else if (j < m) {
                    minAns = (b[j] != c[k]) + prev[i][j + 1];
                }

                curr[i][j] = minAns;
            }
        }
        prev = curr;
    }

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

int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}