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;

    }

    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();

    }

}

Submission (verdict: Accepted):
#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;

}

void solve()

{

    string a,b,s;

    cin>>a>>b>>s;

    ll n=a.length();

    ll m=b.length();

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

    dp[0][0]=0;

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

    {

        dp[i][0]=dp[i-1][0]+(a[i-1]!=s[i-1]);

    }

    for(ll j=1;j<=m;j++)

    {

        dp[0][j]=dp[0][j-1]+(b[j-1]!=s[j-1]);

    }

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

    {

        for(ll j=1;j<=m;j++)

        {



            dp[i][j]=min(dp[i-1][j]+(a[i-1]!=s[i+j-1]),dp[i][j-1]+(b[j-1]!=s[i+j-1]));

        }

    }

    cout<<dp[n][m]<<endl;

    return;

}

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);

    ll t;

    cin >> t;

    while (t--)

    {

        solve();

    }

}