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

Previous submission (verdict: Time limit exceeded on test 16):
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <unordered_map>

using namespace std;

#define FAST_IO ios::sync_with_stdio(false); cin.tie(nullptr);

// Shortcuts
#define int long long
#define vl vector<long long>
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define invl(v, n) rep(i, n) cin >> v[i];

// Loop macros
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep_range(i, a, b) for(int i = a; i < b; ++i)
#define rrep(i, n) for(int i = n - 1; i >= 0; --i)
#define for_each(x, container) for(auto &x : container)

// Memoization maps
unordered_map<string, int> memoHey;
unordered_map<string, int> memoEditDist;

// Helper function to create a unique key for memoization
string getKey(int m, int k) {
    return to_string(m) + "," + to_string(k);
}

string getKey(int m, int n, int k) {
    return to_string(m) + "," + to_string(n) + "," + to_string(k);
}

// Sieve of primes
vector<bool> sieve(int n) {
    vector<bool> is_prime(n + 1, true);
    is_prime[0] = is_prime[1] = false;

    rep(i, sqrt(n) + 1) {
        if (is_prime[i]) {
            for (int j = i * i; j <= n; j += i) {
                is_prime[j] = false;
            }
        }
    }
    return is_prime;
}


int hey(string &a, string &c, int m, int k) {
    // Create a unique key for the current state
    string key = to_string(m) + "," + to_string(k);
    
    // Check if the result for the current state is already computed
    if (memoHey.count(key)) 
        return memoHey[key];

    // Base case: if `m` is 0, return `k`
    if (m == 0) 
        return memoHey[key] = k;

    // If last characters match, recursively compute for the reduced size
    if (a[m - 1] == c[k - 1]) 
        return memoHey[key] = hey(a, c, m - 1, k - 1);

    // If last characters do not match, compute with the operation and store the result
    return memoHey[key] = 1 + hey(a, c, m - 1, k - 1);
}

// Memoized editDist function
int editDist(string &a, string &b, string &c, int m, int n, int k) {
    string key = getKey(m, n, k);
    if (memoEditDist.count(key)) return memoEditDist[key];

    if (m == 0){
        memoHey.clear();
        return memoEditDist[key] = hey(b, c, n, k);

    }
    if (n == 0){
        memoHey.clear();
        return memoEditDist[key] = hey(a, c, m, k);

    }
        
    

    if (a[m - 1] == c[k - 1] && b[n - 1] != c[k - 1])
        return memoEditDist[key] = min(
            editDist(a, b, c, m - 1, n, k - 1),
            1 + editDist(a, b, c, m, n - 1, k - 1)
        );

    if (b[n - 1] == c[k - 1] && a[m - 1] != c[k - 1])
        return memoEditDist[key] = min(
            editDist(a, b, c, m, n - 1, k - 1),
            1 + editDist(a, b, c, m - 1, n, k - 1)
        );

    if (b[n - 1] == c[k - 1] && a[m - 1] == c[k - 1])
        return memoEditDist[key] = min(
            editDist(a, b, c, m, n - 1, k - 1),
            editDist(a, b, c, m - 1, n, k - 1)
        );

    return memoEditDist[key] = 1 + min(
        editDist(a, b, c, m, n - 1, k - 1),
        editDist(a, b, c, m - 1, n, k - 1)
    );
}

signed main() {
    FAST_IO
    int t;
    cin >> t;
    while (t--) {
        string a, b, c;
        cin >> a >> b >> c;
        memoHey.clear();
        memoEditDist.clear();
        int ans = editDist(a, b, c, a.size(), b.size(), c.size());
        cout << ans << endl;
    }
    return 0;
}


Submission (verdict: Accepted):
#include <iostream>
#include <algorithm>

static const int inf = 1e9;

void solve() {
    std::string a, b, res;
    std::cin >> a >> b >> res;
    int n = (int) a.size(), m = (int) b.size();
    int dp[n + 1][m + 1];
    std::fill(&dp[0][0], &dp[0][0] + (n + 1) * (m + 1), inf);
    dp[0][0] = 0;
    for (int i = 0; i < n; i++) {
        dp[i + 1][0] = dp[i][0] + (a[i] != res[i]);
    }
    for (int j = 0; j < m; j++) {
        dp[0][j + 1] = dp[0][j] + (b[j] != res[j]);
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            dp[i][j] = std::min(dp[i - 1][j] + (a[i - 1] != res[i + j - 1]),
                                dp[i][j - 1] + (b[j - 1] != res[i + j - 1]));
        }
    }
    std::cout << dp[n][m] << std::endl;
}

int main() {
    int tests;
    std::cin >> tests;
    while (tests--) {
        solve();
    }
}