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

Previous submission (verdict: Time limit exceeded on test 16):
#include <bits/stdc++.h>
#define ll long long int
#define vl vector<long long>
#define all(x) x.begin(), x.end()
#define in cin >>
#define out cout <<
#define br cout << "\n";
#define sz(x) (ll)(x).size()
#define yes() \
    out "YES";

#define no() \
    out "NO";
#define vvl vector<vl>
#define b() begin()
#define e() end()
#define fac(i, a, c) for (long long i = (a); i < (c); i++)
#define f(i, a) for (long long i = 0; i < (a); i++)
#define fa(v) for (auto &it : (v))
#define pb push_back
#define fi first
#define se second
#define spi set<pair<ll, ll>>
#define next(x) next_permutation(all(x))
#define tc \
    ll t;  \
    in t;  \
    while (t--)
using namespace std;

ll lcs(string X, string Y, string Z, ll m, ll n, ll o, vvl &dp)
{
    // ll o = m + n;
    if (m == 0 and n == 0 and o == 0)
        return 0;
    if (dp[m][n] != -1)
        return dp[m][n];

    ll ans = INT_MAX;

    // Check if current characters match with Z
    if (m - 1 >= 0 and X[m - 1] == Z[o - 1])
        ans = min(ans, lcs(X, Y, Z, m - 1, n, o - 1, dp));

    if (n - 1 >= 0 and Y[n - 1] == Z[o - 1])
        ans = min(ans, lcs(X, Y, Z, m, n - 1, o - 1, dp));

    if (m - 1 >= 0 and X[m - 1] != Z[o - 1])
        ans = min(ans, lcs(X, Y, Z, m - 1, n, o - 1, dp) + 1);

    if (n - 1 >= 0 and Y[n - 1] != Z[o - 1])
        ans = min(ans, lcs(X, Y, Z, m, n - 1, o - 1, dp) + 1);

    return dp[m][n] = ans;
}

void solve()
{
    string a, b, c;
    in a >> b >> c;
    ll la = sz(a), lb = sz(b), lc = sz(c);

    // Create a 3D DP array
    vvl dp(la + 1, vl(lb + 1, -1));

    ll ans = lcs(a, b, c, la, lb, lc, dp);
    out ans;
    br
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out1.txt", "w", stdout);
#endif
    tc { solve(); }
}


Submission (verdict: Memory limit exceeded on test 14):
#include <bits/stdc++.h>
#define ll long long int
#define vl vector<long long>
#define all(x) x.begin(), x.end()
#define in cin >>
#define out cout <<
#define br cout << "\n";
#define sz(x) (ll)(x).size()
#define yes() out "YES";
#define no() out "NO";
#define vvl vector<vl>
#define pb push_back
#define fi first
#define se second
#define tc \
    ll t;  \
    in t;  \
    while (t--)
using namespace std;

ll lcs_tabular(string X, string Y, string Z)
{
    ll m = sz(X), n = sz(Y), o = sz(Z);

    // Create a 3D DP table to store results
    vector<vector<vector<ll>>> dp(m + 1, vector<vector<ll>>(n + 1, vector<ll>(o + 1, INT_MAX)));

    // Base case: If Z is empty, no cost is required
    for (ll i = 0; i <= m; i++)
    {
        for (ll j = 0; j <= n; j++)
        {
            dp[i][j][0] = 0;
        }
    }

    // Fill the DP table
    for (ll k = 1; k <= o; k++)
    {
        for (ll i = 0; i <= m; i++)
        {
            for (ll j = 0; j <= n; j++)
            {

                // If both X and Y contribute to Z[k-1]
                if (i > 0 && X[i - 1] == Z[k - 1])
                {
                    dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j][k - 1]);
                }

                if (j > 0 && Y[j - 1] == Z[k - 1])
                {
                    dp[i][j][k] = min(dp[i][j][k], dp[i][j - 1][k - 1]);
                }

                // If X contributes but does not match Z[k-1]
                if (i > 0 && X[i - 1] != Z[k - 1])
                {
                    dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j][k - 1] + 1);
                }

                // If Y contributes but does not match Z[k-1]
                if (j > 0 && Y[j - 1] != Z[k - 1])
                {
                    dp[i][j][k] = min(dp[i][j][k], dp[i][j - 1][k - 1] + 1);
                }
            }
        }
    }

    return dp[m][n][o];
}

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

    ll ans = lcs_tabular(a, b, c);
    out ans;
    br
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out1.txt", "w", stdout);
#endif
    tc { solve(); }
}