Problem: 2050E - Three Strings
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 16):
#include "bits/stdc++.h"

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define yes return void(cout << "YES\n");
#define no return void(cout << "NO\n");
#define YN(X) cout << ( X ? "Yes" : "No" ) << endl;
#define fr(n) for(int i = 0 ; i < n ; i++)
#define frj(n) for(int j = 0 ; j < n ; j++)
#define fra(u, v) for(auto u : v)
#define fr1(n) for(int i = 1 ; i <= n ; i++)
#define frj1(n) for(int j = 1 ; j <= n ; j++)
#define ret(ans) return void(cout << ans << endl);
#define files freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define looz cin.tie(0), cout.tie(0), cin.sync_with_stdio(0), cout.sync_with_stdio(0);
#define int long long
#define ll long long
#define ld long double
#define endl '\n'

const int MAX = 1e3 + 7;

string a, b, c;
int dp[MAX][MAX], n, m;

int calc(int i, int j, int k){
    if (i == c.size()) return 0;
    int &ret = dp[i][j];
    if (~ret) return ret;
    ret = INT_MAX;
    if (j < n && c[i] == a[j]){
        ret = min(ret, calc(i + 1, j + 1, k));
    }
    if (k < m && c[i] == b[k]){
        ret = min(ret, calc(i + 1, j, k + 1));
    }
    if (j < n && c[i] != a[j]){
        ret = min(ret, calc(i + 1, j + 1, k) + 1);
    }
    if (k < m && c[i] != b[k]){
        ret = min(ret, calc(i + 1, j, k + 1) + 1);
    }
    return ret;
}

void solve() {
    cin >> a >> b >> c;
    n = a.size(), m = b.size();
    fr(c.size() + 3)frj(c.size() + 3) dp[i][j] = -1;
    cout << calc(0, 0, 0) << endl;
}

signed main() {
    looz
//    files
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


Submission (verdict: Accepted):
#include "bits/stdc++.h"

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define yes return void(cout << "YES\n");
#define no return void(cout << "NO\n");
#define YN(X) cout << ( X ? "Yes" : "No" ) << endl;
#define fr(n) for(int i = 0 ; i < n ; i++)
#define frj(n) for(int j = 0 ; j < n ; j++)
#define fra(u, v) for(auto u : v)
#define fr1(n) for(int i = 1 ; i <= n ; i++)
#define frj1(n) for(int j = 1 ; j <= n ; j++)
#define ret(ans) return void(cout << ans << endl);
#define files freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define looz cin.tie(0), cout.tie(0), cin.sync_with_stdio(0), cout.sync_with_stdio(0);
#define int long long
#define ll long long
#define ld long double
#define endl '\n'

const int INF = INT_MAX;
const int N = 4e3 + 10;

string a, b, c;
int dp[N][N];

int get(int i, int j, int k) {
    if (i == c.size()) {
        return 0;
    }
    int &ret = dp[i][j];
    if (~ret) {
        return ret;
    }
    ret = INF;
    if (j < a.size() && c[i] == a[j]) {
        ret = min(ret, get(i + 1, j + 1, k));
    }
    if (k < b.size() && c[i] == b[k]) {
        ret = min(ret, get(i + 1, j, k + 1));
    }
    if (j < a.size() && c[i] != a[j]) {
        ret = min(ret, get(i + 1, j + 1, k) + 1);
    }
    if (k < b.size() && c[i] != b[k]) {
        ret = min(ret, get(i + 1, j, k + 1) + 1);
    }
    return ret;
}

void solve() {
    cin >> a >> b >> c;
    int m = c.size();
    for (int i = 0; i < m + 5; i++) {
        for (int j = 0; j < m + 5; j++) {
            dp[i][j] = -1;
        }
    }
    cout << get(0, 0, 0) << endl;
}


signed main() {
    looz
//    files
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}