Problem: 2050E - Three Strings
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Time limit exceeded on test 16):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MOD = 1e9 + 7;

vector<ll> factorial, inverse;

ll power(ll base, ll exp)

{

    ll res = 1;

    while (exp > 0)

    {

        if (exp % 2 == 1)

        {

            res = (res * base) % MOD;

        }

        base = (base * base) % MOD;

        exp /= 2;

    }

    return res;

}

void precompute(int n)

{

    factorial.resize(n + 1);

    inverse.resize(n + 1);

    factorial[0] = 1;

    for (int i = 1; i <= n; i++)

    {

        factorial[i] = (factorial[i - 1] * i) % MOD;

    }

    inverse[n] = power(factorial[n], MOD - 2);

    for (int i = n - 1; i >= 0; i--)

    {

        inverse[i] = (inverse[i + 1] * (i + 1)) % MOD;

    }

}

ll nCr(int n, int r)

{

    if (r > n || r < 0)

    {

        return 0;

    }

    return (factorial[n] * inverse[r] % MOD * inverse[n - r] % MOD) % MOD;

}



ll f(ll i,ll j,string a,string b,string s,ll n,ll m,vector<vector<ll>>&dp)

{

    if(dp[i][j]!=-1)

    {

        return dp[i][j];

    }

    if(i==n)

    {

        ll count=0;

        while(j!=m)

        {

            if(s[i+j]!=b[j])

            {

                count++;

            }

            j++;

        }

        return count;

    }

    if(j==m)

    {

        ll count=0;

        while(i!=n)

        {

            if(s[i+j]!=a[i])

            {

                count++;

            }

            i++;

        }

        return count;

    }

    ll x=INT64_MAX,y=INT64_MAX;

    if(s[i+j]==a[i])

    {

        x=f(i+1,j,a,b,s,n,m,dp);

    }

    else

    {

        x=f(i+1,j,a,b,s,n,m,dp)+1;

    }

    if(s[i+j]==b[j])

    {

        y=f(i,j+1,a,b,s,n,m,dp);

    }

    else

    {

        y=f(i,j+1,a,b,s,n,m,dp)+1;

    }

    return dp[i][j]=min(x,y);

}

void solve()

{

    string a;

    string b;

    string s;

    cin>>a;

    cin>>b;

    cin>>s;

    ll n=a.length();

    ll m=b.length();

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

    cout<<f(0,0,a,b,s,n,m,dp)<<endl;

}

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);

    ll t;

    cin >> t;

    while (t--)

    {

        solve();

    }

}

Submission (verdict: Time limit exceeded on test 16):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MOD = 1e9 + 7;

vector<ll> factorial, inverse;

ll power(ll base, ll exp)

{

    ll res = 1;

    while (exp > 0)

    {

        if (exp % 2 == 1)

        {

            res = (res * base) % MOD;

        }

        base = (base * base) % MOD;

        exp /= 2;

    }

    return res;

}

void precompute(int n)

{

    factorial.resize(n + 1);

    inverse.resize(n + 1);

    factorial[0] = 1;

    for (int i = 1; i <= n; i++)

    {

        factorial[i] = (factorial[i - 1] * i) % MOD;

    }

    inverse[n] = power(factorial[n], MOD - 2);

    for (int i = n - 1; i >= 0; i--)

    {

        inverse[i] = (inverse[i + 1] * (i + 1)) % MOD;

    }

}

ll nCr(int n, int r)

{

    if (r > n || r < 0)

    {

        return 0;

    }

    return (factorial[n] * inverse[r] % MOD * inverse[n - r] % MOD) % MOD;

}



ll f(ll i,ll j,string a,string b,string s,ll n,ll m,vector<vector<ll>>&dp)

{

    if(dp[i][j]!=-1)

    {

        return dp[i][j];

    }

    if(i==n)

    {

        ll count=0;

        while(j!=m)

        {

            if(s[i+j]!=b[j])

            {

                count++;

            }

            j++;

        }

        return count;

    }

    else if(j==m)

    {

        ll count=0;

        while(i!=n)

        {

            if(s[i+j]!=a[i])

            {

                count++;

            }

            i++;

        }

        return count;

    }

    return dp[i][j]=min(f(i+1,j,a,b,s,n,m,dp)+(s[i+j]!=a[i]),f(i,j+1,a,b,s,n,m,dp)+(s[i+j]!=b[j]));

}

void solve()

{

    string a;

    string b;

    string s;

    cin>>a;

    cin>>b;

    cin>>s;

    ll n=a.length();

    ll m=b.length();

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

    cout<<f(0,0,a,b,s,n,m,dp)<<endl;

}

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);

    ll t;

    cin >> t;

    while (t--)

    {

        solve();

    }

}