Problem: 2050E - Three Strings
Language: c++17 

Solution 1 (runtime: 109 ms):
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
#define int long long
using namespace __gnu_pbds;
const int N=1e6,mod=1e9+7;
 
// clang-format off
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
vector<bool> isprm; void seive() {isprm.resize(N+1,true);for (int i = 2; i * i < N; i++) if (isprm[i]) { for (int j = i * i; j < N; j += i) { isprm[j] = false; }}}
vector<int> spf; void Spf(int n) { spf.resize(n+1); spf[1] = 1; for (int i = 2; i < n; i++) { if (spf[i] == 0) spf[i] = i; for (int j = i * i; j < n; j += i) { if (spf[j] == 0) spf[j] = i; }}}
vector<pair<int, int>> pf; void Pf(int n) { pf.clear(); while (n != 1) { int vl = spf[n], cnt = 0; while (n % vl == 0) { n /= vl; cnt++; } pf.push_back({vl, cnt}); }}
vector<int> factors; void facto(int n) { factors = {1};  for (auto &vl : pf) { int prm = vl.first, cnt = vl.second, prmVal = 1; vector<int> tmp; for (int i = 0; i <= cnt; i++, prmVal *= prm) { for (auto v : factors) tmp.push_back(v * prmVal); } factors = tmp; } sort(factors.begin(), factors.end()); }
vector<pair<int, int>> PrmFct; void primeFactors(int n) { PrmFct.clear(); for (int i = 2; i * i <= n; ++i) { int count = 0; while (n % i == 0) { n /= i; count++; } if (count > 0) PrmFct.push_back({i, count}); } if (n > 1) PrmFct.push_back({n, 1}); }
 
int phi(int n) { int result = n; for (int i = 2; i * i <= n; ++i) { if (n % i == 0) { while (n % i == 0) n /= i; result -= result / i; }} if (n > 1) result -= result / n; return result; } // (calculate count of numbers gcd(i, n) = 1)
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int lcm(int a, int b) { return (a / gcd(a, b)) * b; } int bin_exp(int a, int b, int mod) { int result = 1; a %= mod; while (b > 0) { if (b % 2) result = (result * a) % mod; a = (a * a) % mod; b /= 2; } return result; }
int mod_add(int a, int b, int mod) { return (a % mod + b % mod) % mod; } int mod_sub(int a, int b, int mod) { return (a % mod - b % mod + mod) % mod; } int mod_mul(int a, int b, int mod) { return (a % mod * b % mod) % mod; } int mod_inv(int a, int mod) { return bin_exp(a, mod - 2, mod); } int mod_div(int a, int b, int mod) { return (a % mod * mod_inv(b, mod)) % mod; }
int divisor_sum() { int sum = 1; for (auto p : pf) sum = (sum * mod_div((1 - bin_exp(p.first, p.second + 1, mod) + mod) % mod, (1 - p.first + mod) % mod, mod)) % mod; return sum; }
int divisor_count() { int count = 1; for (auto p :pf) count = (count * ((p.second + 1) % mod) % mod); return count; }
int divisor_product() { int product = 1, cnt = 1; for (auto p : pf) product = bin_exp(product, p.second + 1, mod) * bin_exp(bin_exp(p.first, (p.second * (p.second + 1) / 2), mod), cnt, mod) % mod, cnt = (cnt * (p.second + 1) % (mod - 1)); return product; }
vector<int> factorial; void find_factorials(int n) { factorial.resize(n + 1); factorial[0] = factorial[1] = 1; for (int i = 2; i <= n; i++) factorial[i] = (factorial[i - 1] * 1LL * i) % mod; }
vector<int> factorialInv; void find_factorialsInverse(int n) { factorialInv.resize(n + 1); factorialInv[n] = bin_exp(factorial[n], mod - 2, mod); for (int i = n - 1; i >= 0; i--) factorialInv[i] = (factorialInv[i + 1] * 1LL * (i + 1)) % mod; }
int ncr(int a, int b) { return (b > a) ? 1 : (((factorial[a] * 1LL * factorialInv[b>=1?b:1]) % mod) * factorialInv[a - b]) % mod; }
vector<int> factorialNoMod; void find_factorialsNoMod(int n) { factorialNoMod.resize(n + 1); factorialNoMod[0] = factorialNoMod[1] = 1; for (int i = 2; i <= n; i++) factorialNoMod[i] = factorialNoMod[i - 1] * i; }
int ncrNoMod(int n, int r) { if (r > n) return 0; if (r == 0 || r == n) return 1; r = min(r, n - r); int result = 1; for (int i = 0; i < r; i++) { result *= (n - i); result /= (i + 1); } return result; }
vector<int> hash1, hash2, power1, power2;int base1 = 31, mod1 = 1e9 + 7;int base2 = 37, mod2 = 998244353;
void StringHsh(const string& s) { int n = s.length(); hash1.resize(n + 1, 0); hash2.resize(n + 1, 0); power1.resize(n + 1, 1); power2.resize(n + 1, 1); for (int i = 1; i <= n; i++) { power1[i] = (1LL * power1[i - 1] * base1) % mod1; power2[i] = (1LL * power2[i - 1] * base2) % mod2; hash1[i] = (1LL * hash1[i - 1] * base1 + (s[i - 1] - 'a' + 1)) % mod1; hash2[i] = (1LL * hash2[i - 1] * base2 + (s[i - 1] - 'a' + 1)) % mod2; } }
pair<int, int> getHsh(int l, int r) { int h1 = (hash1[r] - 1LL * hash1[l - 1] * power1[r - l + 1] % mod1 + mod1) % mod1;int h2 = (hash2[r] - 1LL * hash2[l - 1] * power2[r - l + 1] % mod2 + mod2) % mod2;return {h1, h2};}
struct HashPair {template <typename T1, typename T2>std::size_t operator ()(const std::pair<T1, T2>& p) const {auto h1 = std::hash<T1>{}(p.first); auto h2 = std::hash<T2>{}(p.second); return h1 ^ (h2 << 1);}};
// clang-format on
 
int n,m;
string a;
string b;
string c;
int rec(int i,int j,vector<vector<int>>&dp){
    int k=i+j;
    if(k==(n+m)) return 0;
    if (dp[i][j] != -1) return dp[i][j];
    int ans=1e9;
    if (i < n && a[i] == c[k]) 
        ans = min(ans, rec(i + 1, j,dp));

    if (j < m && b[j] == c[k]) 
        ans = min(ans, rec(i, j + 1,dp));

    if (i < n) 
        ans = min(ans, 1 + rec(i + 1, j,dp));

    if (j < m) 
        ans = min(ans, 1 + rec(i, j + 1,dp));

    return dp[i][j] = ans;
}
void solve() {
    cin>>a>>b>>c;
    n=a.size();
    m=b.size();
    vector<vector<int>>dp(n+1,vector<int>(m+1,-1));
    cout<<rec(0,0,dp)<<endl;
}
 
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1; 
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Solution 2 (runtime: 124 ms):
#include <bits/stdc++.h>

using namespace std;

#define el "\n"

#define sz size()

#define pb push_back

#define int long long

#define ll long long

#define fr(n, i) for (int i = 0; i < n; i++)

#define fr1(n, i) for (int i = 1; i < n; i++)

#define all(zoz) zoz.begin(), zoz.end()

#define allr(zoz) zoz.rbegin(), zoz.rend()

#define ones(x) __builtin_popcount(x)

#define fast_zoz ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0)

void zoz() {

#ifndef ONLINE_JUDGE

    freopen("inp.txt", "r", stdin);

    freopen("outp.txt", "w", stdout);

#endif

}

/*   وَلَا تَيْأَسُوا مِن رَّوْحِ اللَّهِ ۖ إِنَّهُ لَا يَيْأَسُ مِن رَّوْحِ اللَّهِ إِلَّا الْقَوْمُ الْكَافِرُون   */

int n, m, k;

const int Mod = 1e9 + 7, M = 2e5;

const int N = 2e5 + 8;

ll OO = 1e18;

// 998244353 1000000007

// int OO = 0x3f3f3f3f;

vector<vector<int> >dp;

string a, b, c;

int clc(int i, int j) {

    if (i + j == n + m)return 0;

    int& ret = dp[i][j];

    if (~ret) return ret;

    ret = OO;

    if (i < n) {

        ret = min(ret, 1 + clc(i + 1, j));

        if (a[i] == c[i + j])ret = min(ret, clc(i + 1, j));

    }

    if (j < m) {

        ret = min(ret, 1 + clc(i, j + 1));

        if (b[j] == c[i + j])ret = min(ret, clc(i, j + 1));

    }

    return ret;

}

void ziad() {

    cin >> a >> b >> c;

    n = a.sz;

    m = b.sz;

    dp = vector<vector<int> >(n + 1, vector<int>(m + 1, -1));

    cout << clc(0, 0) << el;



}

signed main() {

    fast_zoz;

    zoz();

    int testcase = 1;

    cin >> testcase;

    for (int i = 1; i <= testcase; i++) {

        ziad();

    }

    return 0;

}