Problem: 2050E - Three Strings
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Time limit exceeded on test 16):
#include <bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using ll = long long;

const int INF = 1e9;

void solve() {
    string a, b, c;
    cin >> a >> b >> c;

    int n = c.length(), m = a.length(), p = b.length();

    // dp[i][j]: Minimum cost to align the first `i` characters of `c` using first `j` characters of `a`
    vector<vector<int>> dp(m + 1, vector<int>(p + 1, INF));

    // Initialize base cases
    dp[0][0] = 0; // No cost to align 0 characters

    for (int ind = 0; ind < n; ++ind) {
        vector<vector<int>> new_dp(m + 1, vector<int>(p + 1, INF));
        for (int i = 0; i <= m; ++i) {
            for (int j = 0; j <= p; ++j) {
                if (dp[i][j] == INF) continue;

                // Option 1: Take from `a`
                if (i < m) {
                    if (c[ind] == a[i]) 
                        new_dp[i + 1][j] = min(new_dp[i + 1][j], dp[i][j]);
                    else 
                        new_dp[i + 1][j] = min(new_dp[i + 1][j], dp[i][j] + 1);
                }

                // Option 2: Take from `b`
                if (j < p) {
                    if (c[ind] == b[j]) 
                        new_dp[i][j + 1] = min(new_dp[i][j + 1], dp[i][j]);
                    else 
                        new_dp[i][j + 1] = min(new_dp[i][j + 1], dp[i][j] + 1);
                }
            }
        }
        dp = move(new_dp); // Update for the next character
    }

    // Find the minimum cost to align all characters of `c`
    int result = INF;
    for (int i = 0; i <= m; ++i) {
        for (int j = 0; j <= p; ++j) {
            result = min(result, dp[i][j]);
        }
    }

    cout << result << endl;
}

int main() {
    IOS;
    ll t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, k, n) for (long long i = k; i < n; ++i)
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using ll = long long;

const int MOD = 1e9 + 7;

int helper(int ind, int i, int j, string &a, string &b, string &c, vector<vector<int>>&dp) {
    if (ind == c.length()) return 0;
    if (dp[i][j] != -1) return dp[i][j]; 

    int from_a = INT_MAX, from_b = INT_MAX;

    if (i < a.length()) {
        if (c[ind] == a[i])
            from_a = helper(ind + 1, i + 1, j, a, b, c, dp);
        else
            from_a = 1 + helper(ind + 1, i + 1, j, a, b, c, dp);
    }

    if (j < b.length()) {
        if (c[ind] == b[j])
            from_b = helper(ind + 1, i, j + 1, a, b, c, dp);
        else
            from_b = 1 + helper(ind + 1, i, j + 1, a, b, c, dp);
    }

    return dp[i][j] = min(from_a, from_b);
}

void solve() {
    string a, b, c;
    cin >> a >> b >> c;
    int n = c.length();
    vector<vector<int>>dp(a.length() + 1, vector<int>(b.length() + 1, -1));

    int ans = helper(0, 0, 0, a, b, c, dp);
    cout << ans << endl;
}

int main() {
    IOS;
    ll t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}