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

Previous 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(); }
}


Submission (verdict: Accepted):
#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);

    // Create a 2D DP table to store results
    vector<vector<ll>> dp(m + 1, vector<ll>(n + 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++)
        {
            if (i + j == 0) // Z is empty
                dp[i][j] = 0;
        }
    }

    // Fill the DP table
    for (ll i = 0; i <= m; i++)
    {
        for (ll j = 0; j <= n; j++)
        {
            ll k = i + j; // Current index in Z
            if (k > sz(Z))
                continue;

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

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

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

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

    return dp[m][n];
}

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(); }
}