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

Previous submission (verdict: Memory limit exceeded on test 14):
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
 
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
 
/*
------------------------------------------
Author       :- Shadan Hussain
Started      :- [04-01-24]
College      :- NIT Rourkela
------------------------------------------
*/
 
#define int long long int
#define el '\n'
#define take(x) int x; cin>>x;
#define getv(v, n)  vector<int> v;for (int i = 0; i < n; i++){ take(x) v.push_back(x);}
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define sortv(v) sort(all(v))
#define sortvr(v) sort(allr(v))
#define gcd(a,b)  __gcd(a,b)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define pb push_back
#define ppb pop_back
#define yes cout << "YES" << el
#define no cout << "NO" << el
#define ff first
#define ss second
#define MOD 1000000007
#define INF 1e18
#define ppc(x) __builtin_popcountll(x)
#define bitsparity(x) __builtin_parityll(x) //1/true for odd , 0/false for even
#define par(x) (x&1) //1/true for odd , 0/false for even
#define trail(x) __builtin_ctzll(x)
#define lead(x) __builtin_clzll(x)
#define vecmin(x) *min_element(all(x)) // store it in a variable, then use the value
#define vecmax(x) *max_element(all(x)) // store it in a variable, then use the value
#define vecsum(x) accumulate(all(x), 0ll);
#define vecpre(x) partial_sum(all(x), x.begin())
#define vecsuf(x) partial_sum(allr(x), x.rbegin())
#define bug(x) cout<<#x<<"->"<<x<<el;
 
typedef long long ll; //-1e18 -> 1e18
typedef unsigned long long ull;  //0 -> 1e19
typedef long double lld;
typedef pair<int, int> pii;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key
typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update > pbdsg; // find_by_order, order_of_key
//pbds for increasing order, pbdsg for descending order, change data types for other usage
 
void fastio()
{ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); cout.precision(10); srand(chrono::high_resolution_clock::now().time_since_epoch().count());}
 
 bool cmp(pair<int,int>a,pair<int,int>b){
    if(a.first!=b.first)
    return a.first<b.first;
    else
    return a.second>b.second;
 }
//  int reverse(int n){
//     int ans=0;
//     while(n>0){
//         int d=n%10;
//         ans=ans*10+d;

//         n/=10;
//     }
//     return ans;
//  }

int f(string &s1,string& s2,string &s3,int i,int j,int k,vector<vector<vector<int>>>&dp){
  int n=s1.size();
  int m=s2.size();
  int len=s3.size();
  if(k==len)
  return 0;
  if(dp[i][j][k]!=-1)
  return dp[i][j][k];
  int ans=1e9;
  if(j<m)
   ans=min(ans,1+f(s1,s2,s3,i,j+1,k+1,dp));
   if(i<n)
    ans=min(ans,1+f(s1,s2,s3,i+1,j,k+1,dp));

    if((i<n && s1[i]==s3[k]))
      ans=min(ans,f(s1,s2,s3,i+1,j,k+1,dp));
    if(j<m && s2[j]==s3[k])
    ans=min(ans,f(s1,s2,s3,i,j+1,k+1,dp));

  return dp[i][j][k]= ans;
}
void shadan()
{
  string a,b,c;
  cin>>a>>b>>c;
  int n=a.size();
  int m=b.size();
  int len=c.size();
  vector<vector<vector<int>>>dp(n+1,vector<vector<int>>(m+1,vector<int>(len+1,-1)));
  cout<<f(a,b,c,0,0,0,dp)<<endl;
  
 
}
signed main()
{
//   #ifndef ONLINE_JUDGE
//     //for getting input from input.txt
//     freopen("input1.txt", "r", stdin);
//     //for writing output to output.txt
//     freopen("output1.txt", "w", stdout);
// #endif
  fastio();
  int T = 1;
  cin >> T;
  for (int tc = 1; tc <= T; tc++)
  {
    /*cout << "Case #" << tc << ": ";*/
      shadan();
  }
  return 0;
}
//Snippet path: C:\Users\USER\AppData\Roaming\Sublime Text 3\Packages\User

Submission (verdict: Time limit exceeded on test 16):
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
 
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
 
/*
------------------------------------------
Author       :- Shadan Hussain
Started      :- [04-01-24]
College      :- NIT Rourkela
------------------------------------------
*/
 
#define int long long int
#define el '\n'
#define take(x) int x; cin>>x;
#define getv(v, n)  vector<int> v;for (int i = 0; i < n; i++){ take(x) v.push_back(x);}
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define sortv(v) sort(all(v))
#define sortvr(v) sort(allr(v))
#define gcd(a,b)  __gcd(a,b)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define pb push_back
#define ppb pop_back
#define yes cout << "YES" << el
#define no cout << "NO" << el
#define ff first
#define ss second
#define MOD 1000000007
#define INF 1e18
#define ppc(x) __builtin_popcountll(x)
#define bitsparity(x) __builtin_parityll(x) //1/true for odd , 0/false for even
#define par(x) (x&1) //1/true for odd , 0/false for even
#define trail(x) __builtin_ctzll(x)
#define lead(x) __builtin_clzll(x)
#define vecmin(x) *min_element(all(x)) // store it in a variable, then use the value
#define vecmax(x) *max_element(all(x)) // store it in a variable, then use the value
#define vecsum(x) accumulate(all(x), 0ll);
#define vecpre(x) partial_sum(all(x), x.begin())
#define vecsuf(x) partial_sum(allr(x), x.rbegin())
#define bug(x) cout<<#x<<"->"<<x<<el;
 
typedef long long ll; //-1e18 -> 1e18
typedef unsigned long long ull;  //0 -> 1e19
typedef long double lld;
typedef pair<int, int> pii;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key
typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update > pbdsg; // find_by_order, order_of_key
//pbds for increasing order, pbdsg for descending order, change data types for other usage
 
void fastio()
{ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); cout.precision(10); srand(chrono::high_resolution_clock::now().time_since_epoch().count());}
 
 bool cmp(pair<int,int>a,pair<int,int>b){
    if(a.first!=b.first)
    return a.first<b.first;
    else
    return a.second>b.second;
 }
//  int reverse(int n){
//     int ans=0;
//     while(n>0){
//         int d=n%10;
//         ans=ans*10+d;

//         n/=10;
//     }
//     return ans;
//  }

int f(string &s1,string& s2,string &s3,int i,int j,int k,map<vector<int>,int>&dp){
  int n=s1.size();
  int m=s2.size();
  int len=s3.size();
  if(k==len)
  return 0;
  if(dp.find({i,j,k})!=dp.end())
  return dp[{i,j,k}];
  int ans=1e9;
  if(j<m)
   ans=min(ans,1+f(s1,s2,s3,i,j+1,k+1,dp));
   if(i<n)
    ans=min(ans,1+f(s1,s2,s3,i+1,j,k+1,dp));

    if((i<n && s1[i]==s3[k]))
      ans=min(ans,f(s1,s2,s3,i+1,j,k+1,dp));
    if(j<m && s2[j]==s3[k])
    ans=min(ans,f(s1,s2,s3,i,j+1,k+1,dp));

  return dp[{i,j,k}]= ans;
}
void shadan()
{
  string a,b,c;
  cin>>a>>b>>c;
  int n=a.size();
  int m=b.size();
  int len=c.size();
  map<vector<int>,int>dp;
  cout<<f(a,b,c,0,0,0,dp)<<endl;
  
 
}
signed main()
{
//   #ifndef ONLINE_JUDGE
//     //for getting input from input.txt
//     freopen("input1.txt", "r", stdin);
//     //for writing output to output.txt
//     freopen("output1.txt", "w", stdout);
// #endif
  fastio();
  int T = 1;
  cin >> T;
  for (int tc = 1; tc <= T; tc++)
  {
    /*cout << "Case #" << tc << ": ";*/
      shadan();
  }
  return 0;
}
//Snippet path: C:\Users\USER\AppData\Roaming\Sublime Text 3\Packages\User