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

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

int solve(int i, int j, int k, int l1, int l2, int l3, string &s1, string &s2, string &p, vector<vector<int>> &memo) {
    // Base case: if both strings are exhausted
    if (i == l1 && j == l2) {
        return 0;
    }

    // If one string is exhausted, compare the remaining characters of the other string with p
    if (i == l1) {
        int changes = 0;
        while (j < l2) {
            if (s2[j] != p[k]) changes++;
            j++, k++;
        }
        return changes;
    }
    if (j == l2) {
        int changes = 0;
        while (i < l1) {
            if (s1[i] != p[k]) changes++;
            i++, k++;
        }
        return changes;
    }

    // Check if result is already computed
    if (memo[i][j] != -1) {
        return memo[i][j];
    }

    // Recursive case: consider both options (take from s1 or s2)
    int res1 = solve(i + 1, j, k + 1, l1, l2, l3, s1, s2, p, memo) + (s1[i] != p[k]);
    int res2 = solve(i, j + 1, k + 1, l1, l2, l3, s1, s2, p, memo) + (s2[j] != p[k]);

    // Store the result in memo and return the minimum of both options
    return memo[i][j] = min(res1, res2);
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        string s1, s2, p;
        cin >> s1 >> s2 >> p;

        int l1 = s1.length(), l2 = s2.length(), l3 = p.length();

        // Memoization table
        vector<vector<int>> memo(l1 + 1, vector<int>(l2 + 1, -1));

        cout << solve(0, 0, 0, l1, l2, l3, s1, s2, p, memo) << endl;
    }
    return 0;
}


Solution 2 (runtime: 62 ms):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define f(i, a, n) for (ll i = a; i < n; i++)
#define rf(i, n, a) for (ll i = n - 1; i >= a; i--)
#define pb push_back
#define pii pair<int, int>
ll fun(string &a, string &b, string &c, int i, int j, int k)
{
    if (i < 0)
        return 0;
    ll m = 0;
    if (j >= 0)
    {
        if (a[i] == b[j])
            m = 1 + fun(a, b, c, i - 1, j - 1, k);
        else
            m = fun(a, b, c, i - 1, j - 1, k);
    }
    if (k >= 0)
    {
        if (a[i] == c[k])
            m = max(m, 1 + fun(a, b, c, i - 1, j, k - 1));
        else
            m = max(m, fun(a, b, c, i - 1, j, k - 1));
    }
    return m;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin >> t;
    while (t--)
    {
        string b, c, a;
        cin >> b >> c >> a;
        // cout << a.size() - fun(a, b, c, a.size() - 1, b.size() - 1, c.size() - 1) << endl;
        vector<ll> dp(c.size() + 1, 0);
        for (int i = 0; i <= b.size(); i++)
        {
            if (i - 1 >= 0 && a[i - 1] == b[i - 1])
                dp[0]++;
            for (int j = 1; j <= c.size(); j++)
            {
                ll n = dp[j];
                ll m = dp[j - 1];
                if (i + j - 1 >= 0)
                {
                    if (i - 1 >= 0 && a[i + j - 1] == b[i - 1])
                        n++;
                    if (a[i + j - 1] == c[j - 1])
                        m++;
                    dp[j] = max(m, n);
                }
            }
        }
        cout << a.size() - dp[c.size()] << endl;
    }
}