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

Previous submission (verdict: Memory limit exceeded on test 14):
#include <bits/stdc++.h>
using namespace std;

#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef unordered_map<ll, ll> umpl;
typedef unordered_map<int, int> umpi;
typedef map<ll, ll> mpl;
typedef map<int, int> mpi;
typedef priority_queue<ll> pql;
typedef priority_queue<int> pqi;
typedef priority_queue<ll, vector<ll>, greater<ll>> minpql;
typedef priority_queue<int, vector<int>, greater<int>> minpqi;
typedef stack<ll> stl;
typedef stack<int> sti;
const ll mod = 1'000'000'007;
const ll N = 3e5, M = N;
vpii dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vpii dir1 = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
//=======================

vi g[N];

int f(string &a, string &b, string &c, int i, int j, int k, vector<vector<vector<int>>> &vec)
{
    if(k<0)
    {
        return 0;
    }
    if(i<0)
    {
        int ans=0;
        while(j>=0)
        {
            if(b[j]==c[k])
            {
                j--;
                k--;
            }
            else
            {
                j--;
                k--;
                ans++;
            }
        }
        return ans;
    }

    if(j<0)
    {
        int ans=0;
        while(i>=0)
        {
            if(a[i]==c[k])
            {
                i--;
                k--;
            }
            else
            {
                i--;
                k--;
                ans++;
            }
        }
        return ans;
    }
    if(vec[i][j][k]!=-1)
    {
        return vec[i][j][k];
    }
    int takinga=0;
    if(i>=0)
    {
        if(a[i]!=c[k]){
            takinga++;
        }
        takinga += f(a,b,c, i-1, j, k-1, vec);
    }
    else
    {
        takinga=INT_MAX;
    }
    
    int takingb=0;
    if(j>=0)
    {
        if(b[j]!=c[k]){
            takingb++;
        }
        takingb += f(a,b,c, i, j-1, k-1, vec);
    }
    else
    {
        takingb=INT_MAX;
    }
    
    return vec[i][j][k]=min(takinga, takingb);
}
void solve()
{
    ll n, i, k, m, j;
    string a,b,c;
    cin>>a>>b>>c;
    vector<vector<vector<int>>> vec(a.length(), vector<vector<int>> (b.length(), vector<int> (c.length(), -1)));
    cout<<f(a,b,c,a.length()-1, b.length()-1, c.length()-1, vec)<<endl;
    return;
}

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

    ll t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}


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

#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef unordered_map<ll, ll> umpl;
typedef unordered_map<int, int> umpi;
typedef map<ll, ll> mpl;
typedef map<int, int> mpi;
typedef priority_queue<ll> pql;
typedef priority_queue<int> pqi;
typedef priority_queue<ll, vector<ll>, greater<ll>> minpql;
typedef priority_queue<int, vector<int>, greater<int>> minpqi;
typedef stack<ll> stl;
typedef stack<int> sti;
const ll mod = 1'000'000'007;
const ll N = 3e5, M = N;
vpii dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vpii dir1 = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
//=======================

vi g[N];

// int f(string &a, string &b, string &c, int i, int j, int k, vector<vector<vector<int>>> &vec)
int f(string &a, string &b, string &c, int i, int j, int k, vector<vector<int>> &vec)
{
    if(k<0)
    {
        return 0;
    }
    if(i<0)
    {
        int ans=0;
        while(j>=0)
        {
            if(b[j]==c[k])
            {
                j--;
                k--;
            }
            else
            {
                j--;
                k--;
                ans++;
            }
        }
        return ans;
    }

    if(j<0)
    {
        int ans=0;
        while(i>=0)
        {
            if(a[i]==c[k])
            {
                i--;
                k--;
            }
            else
            {
                i--;
                k--;
                ans++;
            }
        }
        return ans;
    }
    if(vec[i][j]!=-1)
    {
        return vec[i][j];
    }
    int takinga=0;
    if(i>=0)
    {
        if(a[i]!=c[k]){
            takinga++;
        }
        takinga += f(a,b,c, i-1, j, k-1, vec);
    }
    else
    {
        takinga=INT_MAX;
    }
    
    int takingb=0;
    if(j>=0)
    {
        if(b[j]!=c[k]){
            takingb++;
        }
        takingb += f(a,b,c, i, j-1, k-1, vec);
    }
    else
    {
        takingb=INT_MAX;
    }
    
    return vec[i][j]=min(takinga, takingb);
}
void solve()
{
    ll n, i, k, m, j;
    string a,b,c;
    cin>>a>>b>>c;
    // vector<vector<vector<int>>> vec(a.length(), vector<vector<int>> (b.length(), vector<int> (c.length(), -1)));
    // cout<<f(a,b,c,a.length()-1, b.length()-1, c.length()-1, vec)<<endl;
    vector<vector<int>> vec(a.length()+1, (vector<int> (b.length()+1, -1) ) );
    cout<<f(a,b,c,a.length()-1, b.length()-1, c.length()-1, vec)<<endl;
    return;
}



int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

    ll t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}