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

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

typedef long long int lli;
const int modd = 1e9 + 7;

#define int long long
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define bac(i, a, b) for (int i = a; i >= b; i--)
#define yes "YES"
#define no "NO"
#define nl " \n"
#define ak(x) cout << x << "\n";
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
template <class T>using max_heap = priority_queue<T>;
template <class T>using min_heap = priority_queue<T, vector<T>, greater<T>>;


int solve(int l,int m,int n,string &a,string &b,string &c,vector<vector<vector<int>>>&dp){
	if(n==0) return 0;
	if(l == 0){
		// cout<<"AK:"<<"M:"<<m<<" N:"<<n<<nl;
		int cnt=0;
		for(int i=0;i<n;i++){
			if(b[i] != c[i]) cnt++;
		}
		return cnt;
	}
	if(m == 0){
		// cout<<"AK:"<<"L:"<<l<<" N:"<<n<<nl;
		int cnt=0;
		for(int i=0;i<n;i++){
			if(a[i] != c[i]) cnt++;
		}
		return cnt;
	}

	if(dp[l][m][n] != -1) return dp[l][m][n];

	if(a[l-1] == c[n-1] && b[m-1] == c[n-1]){
		return dp[l][m][n] = min(solve(l-1,m,n-1,a,b,c,dp),solve(l,m-1,n-1,a,b,c,dp));
	}
	if(a[l-1] == c[n-1]){
		return dp[l][m][n] = min(solve(l-1,m,n-1,a,b,c,dp),1+solve(l,m-1,n-1,a,b,c,dp));
	}
	if(b[m-1] == c[n-1]){
		return dp[l][m][n] = min(solve(l,m-1,n-1,a,b,c,dp),1+solve(l-1,m,n-1,a,b,c,dp));
	}
	else {
		// not matches with anyone then skip it
		return dp[l][m][n] = 1 + min(solve(l-1,m,n-1,a,b,c,dp),solve(l,m-1,n-1,a,b,c,dp));
	}
}

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

	int l=a.size();
	int m=b.size();
	int n=c.size();
	vector<vector<vector<int>>>dp(l+1,vector<vector<int>>(m+1,vector<int>(n+1,-1)));
	cout<<solve(l,m,n,a,b,c,dp)<<nl;
}


int32_t main(){
	fastio();
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif

	int t = 1;
	cin >> t;

	while (t--){
		solve();
	}
}

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

typedef long long int lli;
const int modd = 1e9 + 7;

#define int long long
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define bac(i, a, b) for (int i = a; i >= b; i--)
#define yes "YES"
#define no "NO"
#define nl " \n"
#define ak(x) cout << x << "\n";
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
template <class T>using max_heap = priority_queue<T>;
template <class T>using min_heap = priority_queue<T, vector<T>, greater<T>>;


int solve(int l,int m,int n,string &a,string &b,string &c,vector<vector<int>>&dp){
	if(n==0) return 0;
	if(l == 0){
		// cout<<"AK:"<<"M:"<<m<<" N:"<<n<<nl;
		int cnt=0;
		for(int i=0;i<n;i++){
			if(b[i] != c[i]) cnt++;
		}
		return cnt;
	}
	if(m == 0){
		// cout<<"AK:"<<"L:"<<l<<" N:"<<n<<nl;
		int cnt=0;
		for(int i=0;i<n;i++){
			if(a[i] != c[i]) cnt++;
		}
		return cnt;
	}

	if(dp[l][m] != -1) return dp[l][m];

	if(a[l-1] == c[n-1] && b[m-1] == c[n-1]){
		return dp[l][m] = min(solve(l-1,m,n-1,a,b,c,dp),solve(l,m-1,n-1,a,b,c,dp));
	}
	if(a[l-1] == c[n-1]){
		return dp[l][m] = min(solve(l-1,m,n-1,a,b,c,dp),1+solve(l,m-1,n-1,a,b,c,dp));
	}
	if(b[m-1] == c[n-1]){
		return dp[l][m] = min(solve(l,m-1,n-1,a,b,c,dp),1+solve(l-1,m,n-1,a,b,c,dp));
	}
	else {
		// not matches with anyone then skip it
		return dp[l][m] = 1 + min(solve(l-1,m,n-1,a,b,c,dp),solve(l,m-1,n-1,a,b,c,dp));
	}
}

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

	int l=a.size();
	int m=b.size();
	int n=c.size();
	vector<vector<int>>dp(l+1,vector<int>(m+1,-1));
	cout<<solve(l,m,n,a,b,c,dp)<<nl;
}


int32_t main(){
	fastio();
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif

	int t = 1;
	cin >> t;

	while (t--){
		solve();
	}
}