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

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

int diffCount(string a, string b)
{
    int count = 0;
    for (int i = 0; i < a.size(); i++)
    {
        if (a[i] != b[i])
            count++;
    }
    return count;
}

int dp_helper(string a, string b, string c, int i, int j, int k, vector<vector<int>> &arr)
{
    if(arr[i][j]!= -1){
        return arr[i][j];
    }
    if (i >= a.size() && j >= b.size())
    {
        return 0;
    }
    if (i >= a.size())
    {
        return arr[i][j] = diffCount(b.substr(j, b.size() - j), c.substr(k, c.size() - k));
    }
    else if (j >= b.size())
    {
        return arr[i][j] = diffCount(a.substr(i, a.size() - i), c.substr(k, c.size() - k));
    }
    if(a[i]==c[k] && b[j] == c[k]){
        return arr[i][j] =  min(dp_helper(a, b, c, i + 1, j, k + 1, arr), dp_helper(a, b, c, i, j + 1, k + 1, arr));
    }

    else if (a[i] == c[k])
    {
        return arr[i][j] = min(dp_helper(a, b, c, i + 1, j, k + 1, arr), 1 + dp_helper(a,b,c,i,j+1,k+1, arr));
    }
    else if (b[j] == c[k])
    {
        return arr[i][j] = min(dp_helper(a, b, c, i, j + 1, k + 1, arr), 1 + dp_helper(a, b, c, i + 1, j, k + 1, arr));
    }
    else
    {
        return arr[i][j] =  1 + min(dp_helper(a, b, c, i + 1, j, k + 1, arr), dp_helper(a, b, c, i, j + 1, k + 1, arr));
    }
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        
        string a, b, c;
        cin >> a >> b >> c;
        vector<vector<int>> arr(1002, vector<int> (1002, -1));
        cout << dp_helper(a, b, c, 0, 0, 0, arr)<<endl;
    }
}


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

int diffCount(string a, string b)
{
    int count = 0;
    for (int i = 0; i < a.size(); i++)
    {
        if (a[i] != b[i])
            count++;
    }
    return count;
}

int dp_helper(string &a, string &b, string &c, int i, int j, int k, vector<vector<int>> &arr)
{
    if(arr[i][j]!= -1){
        return arr[i][j];
    }
    if (i >= a.size() && j >= b.size())
    {
        return 0;
    }
    if (i >= a.size())
    {
        return arr[i][j] = diffCount(b.substr(j, b.size() - j), c.substr(k, c.size() - k));
    }
    else if (j >= b.size())
    {
        return arr[i][j] = diffCount(a.substr(i, a.size() - i), c.substr(k, c.size() - k));
    }
    if(a[i]==c[k] && b[j] == c[k]){
        return arr[i][j] =  min(dp_helper(a, b, c, i + 1, j, k + 1, arr), dp_helper(a, b, c, i, j + 1, k + 1, arr));
    }

    else if (a[i] == c[k])
    {
        return arr[i][j] = min(dp_helper(a, b, c, i + 1, j, k + 1, arr), 1 + dp_helper(a,b,c,i,j+1,k+1, arr));
    }
    else if (b[j] == c[k])
    {
        return arr[i][j] = min(dp_helper(a, b, c, i, j + 1, k + 1, arr), 1 + dp_helper(a, b, c, i + 1, j, k + 1, arr));
    }
    else
    {
        return arr[i][j] =  1 + min(dp_helper(a, b, c, i + 1, j, k + 1, arr), dp_helper(a, b, c, i, j + 1, k + 1, arr));
    }
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        
        string a, b, c;
        cin >> a >> b >> c;
        vector<vector<int>> arr(1002, vector<int> (1002, -1));
        cout << dp_helper(a, b, c, 0, 0, 0, arr)<<endl;
    }
}