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

Solution 1 (runtime: 62 ms):
#include <bits/stdc++.h>
using namespace std;

#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

vector<vector<int>> dp;

int solve(string &a, string &b, string &c, int st1, int st2, int st3) {
    if (st1 == a.size() && st2 == b.size()) {
        return 0;
    }

    if (dp[st1][st2] != -1) {
        return dp[st1][st2];
    }

    int take_a = INT_MAX, take_b = INT_MAX;

    if (st3 < c.size() && st1 < a.size() && c[st3] == a[st1]) {
        take_a = solve(a, b, c, st1 + 1, st2, st3 + 1);
    }

    if (st3 < c.size() && st2 < b.size() && c[st3] == b[st2]) {
        take_b = solve(a, b, c, st1, st2 + 1, st3 + 1);
    }

    if (st3 < c.size()) {
        if (st1 < a.size()) {
            take_a = min(take_a, 1 + solve(a, b, c, st1 + 1, st2, st3 + 1));
        }
        if (st2 < b.size()) {
            take_b = min(take_b, 1 + solve(a, b, c, st1, st2 + 1, st3 + 1));
        }
    }

    dp[st1][st2] = min(take_a, take_b);
    return dp[st1][st2];
}

int main() {
    FAST_IO  

    int t;
    cin >> t;
    while (t--) {
        string a, b, c;
        cin >> a >> b >> c;

        dp = vector<vector<int>>(a.size() + 1, vector<int>(b.size() + 1, -1));

        int ans = solve(a, b, c, 0, 0, 0);

        if (ans == INT_MAX) {
            cout << "-1\n";
        } else {
            cout << ans << '\n';
        }
    }

    return 0;
}


Solution 2 (runtime: 609 ms):
#include<bits/stdc++.h>
using namespace std;

// ************// code snippet of ordered_set  //*********************************************

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
 
// typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>, __gnu_pbds::rb_tree_tag, 
// __gnu_pbds::tree_order_statistics_node_update> ordered_set;

// *******************************************************************************************

#define pii pair<int,int>
#define int long long int
#define pb push_back
#define eb emplace_back
#define db long double
#define f first 
#define s second
#define vi vector<int> 
#define vp vector<pii>
#define vc vector<char>
#define str string
#define vb vector<bool> 
#define vvi vector<vector<int>> 
#define vvs vector<vector<str>>
#define vvb vector<vector<bool>> 
#define vvp vector<vector<pii>>
#define vvc vector<vector<char>>
#define vs vector<string>
#define vvvi vector<vector<vector<int>>>
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define frr(i,a,b) for(int i=a;i>=b;i--)
#define frs(i,a,b,k) for(int i=a;i<=b;i+=k)
#define frrs(i,a,b,k) for(int i=a;i>=b;i-=k)
#define rs(a,b) resize(a,b)
#define b() begin()
#define e() end()
#define INF 1e18
const int mod=1e9+7;
const int N=10;

// vector<bool>primes(N,true);
// vector<int>prms(N,-1);

// auto max(auto a,auto b){if(a>b) return a;return b;}
// auto min(auto a,auto b){if(a<b) return a;return b;}
// tuple<int,int,int> eea(int a,int b){if(b==0) return {1,0,a};int z=a;auto t=eea(b,z%b);return {get<1>(t),get<0>(t)-(a/b)*get<1>(t),get<2>(t)};}
// int mul(int a,int b,int p){int val=0;while(b>0){if(b&1) val=(val+a)%p;a=(a+a)%p;b>>=1;}return val;}  
// int gcd(int a,int b){while(a>0 and b>0){if(a>b) a=a%b;else b=b%a;}return a==0?b:a;}
// void matrix_mul(vector<vector<int>>a,vector<vector<int>>&b,vector<vector<int>>&c){int r1=a.size(),c1=a[0].size();int r2=b.size(),c2=b[0].size();c.resize(r1,vector<int>(c2));for(int i=0;i<r1;i++){for(int j=0;j<c1;j++){for(int k=0;k<r2;k++){c[i][j]+=a[i][k]*b[k][j];}}}}
// void sieve(int n){primes[0]=primes[1]=false;for(int i=2;i*i<=n;i++){if(primes[i]){for(int j=i*i;j<=n;j+=i){primes[j]=false;}}}}
// int expo(int a,int b){int val=1;while(b>0){if(b&1) val=val*a;a=a*a;}return val;}
// void sieve(){for(int i=2;i<N;i++){prms[1]=1;if(prms[i]==-1){for(int j=i;j<N;j+=i){if(prms[j]==-1){prms[j]=i;}}}}}

   
// *************// Z-algorithm // ************************************************************
   /*

    vi Z(st.size());
    Z[0]=0; // actually Z[0] is undefined

    int l=0,r=0;
    for(int i=0;i<st.size();i++){
        if(i<=r){
            Z[i]=min(r-i+1,Z[i-l]);
        }
        while(i+Z[i]<st.size() and s[Z[i]]==s[i+Z[i]]) Z[i]++;
        if(i+Z[i]>r+1){
            r=i+Z[i]-1;
            l=i;
        }
    }

    */
// *******************************************************************************************

//***************// kmp algorithm //**********************************************************
    /*
    
    vi lsp(st.size());
    lsp[0]=0;
    int index=0;
    int i=1;
    while(i<st.size()){
        if(st[i]==st[index]) lsp[i++]=++index;
        else{
            if(index) index=lsp[index-1];
            else lsp[i++]=0;
        }
    }
    
    */
//*********************************************************************************************

str a,b,c;
vvvi dp;

void main1(){
    // your code goes here 
    cin>>a>>b>>c;
    int x=a.size(),y=c.size();
    dp.rs(y,vvi(x+1,vi(2,-INF)));
    
    if(c[0]==a[0]) dp[0][0][0]=1;
    else dp[0][0][0]=0;

    if(c[0]==b[0]) dp[0][0][1]=1;
    else dp[0][0][1]=0;

    
    fr(j,1,y-1){
        fr(i,0,x){
            int val=dp[j-1][i][1];
            if(i>0) val=max(dp[j-1][i-1][0],dp[j-1][i][1]); 
            if(i<x) dp[j][i][0]=val+((c[j]==a[i])?1:0);
            if(j-i<b.size()) dp[j][i][1]=val+((c[j]==b[j-i])?1:0);
        }
    }

    // fr(i,0,y-1){
    //     fr(j,0,x){
    //         cout<<"{"<<dp[i][j][0]<<" "<<dp[i][j][1]<<"}  ";
    //     }
    //     cout<<endl;
    // }

    cout<<y-max(dp[y-1][x-1][0],dp[y-1][x][1])<<"\n";
    dp.rs(0,{});
}

int32_t main(){

    // ios_base::sync_with_stdio(false); 
    // cin.tie(NULL);

    int t=1;
    cin>>t;
    for(int i=1;i<=t;i++){
        main1();
    }

   return 0;
}