Problem: 2050E - Three Strings
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 16):
#pragma GCC optimize("O3")
//#pragma gcc target("avx,avx2,fma")
//#pragma gcc optimization ("unroll-loops")
//#pragma gcc target("avx,avx2,sse,sse2,sse3,sse4,popcnt")
#include <bitset>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <cmath>
#include <queue>
#include <deque>
#include <iterator>
#include <fstream>
#include <cstdlib>
#include <stdio.h>
#include <stack>
#include <random>
#include <string>
#include <functional>
#include <chrono>
#include <numeric>
#include <climits>
#include <tuple>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
using namespace std;


typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> pii;

const LL DIM = 1007;
const LL INF = 999999999999999999;
const LL OFFSET = 250000;
const LL MAX_SUM = 500007;

LL F[DIM][DIM];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    LL n, m, t;
    string a, b, s;
    cin >> t;
    for (int T = 1; T <= t; T++) {
        cin >> a >> b >> s;

        n = a.length();
        m = b.length();

        F[0][0] = b[0] == s[0] ? 0 : 1;
        F[0][1] = a[0] == s[0] ? 0 : 1;
        //cout << F[0][0] << " " << F[0][1] << endl;
        for (int i = 2; i < n; i++) {
            F[0][i] = -1;
        }
        for (int l = 1; l < n + m; l++) {
            if (l < m) {
                LL c = b[l] == s[l] ? 0 : 1;
                F[l][0] = F[l - 1][0] + c;
            }
            else F[l][0] = -1;
            //cout << F[l][0] << " ";
            for (int i = 1; i <= l + 1; i++) {
                if (l - i > m || i > n) {
                    F[l][i] = -1;

                    //cout << F[l][i] << " ";
                    continue;
                }
                
                if (l - i < 0) {
                    LL c2 = a[i - 1] == s[l] ? 0 : 1;
                    F[l][i] = F[l - 1][i - 1] + c2;
                }
                else if (i - 1 < 0) {
                    LL c1 = b[l - i] == s[l] ? 0 : 1;
                    F[l][i] = F[l - 1][i] + c1;
                }
                else {
                    LL c1 = b[l - i] == s[l] ? 0 : 1;
                    LL c2 = a[i - 1] == s[l] ? 0 : 1;

                    //if(i == 2 && l == 2) cout << F[l - 1][i] << endl;

                    if (F[l - 1][i] == -1) F[l][i] = F[l - 1][i - 1] + c2;
                    else F[l][i] = min(F[l - 1][i] + c1, F[l - 1][i - 1] + c2);
                }

                //cout << F[l][i] << " ";
            }
            //cout << endl;
            for (int i = l + 2; i < n; i++) F[l][i] = -1;
        }
        cout << F[s.length() - 1][n] << endl;
    }


    return 0;
}

Submission (verdict: Time limit exceeded on test 16):
#pragma GCC optimize("O3")
//#pragma gcc target("avx,avx2,fma")
//#pragma gcc optimization ("unroll-loops")
//#pragma gcc target("avx,avx2,sse,sse2,sse3,sse4,popcnt")
#include <bitset>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <cmath>
#include <queue>
#include <deque>
#include <iterator>
#include <fstream>
#include <cstdlib>
#include <stdio.h>
#include <stack>
#include <random>
#include <string>
#include <functional>
#include <chrono>
#include <numeric>
#include <climits>
#include <tuple>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
using namespace std;


typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> pii;

const LL DIM = 1007;
const LL INF = 999999999999999999;
const LL OFFSET = 250000;
const LL MAX_SUM = 500007;

LL F[DIM][DIM];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    LL n, m, t;
    string a, b, s;
    cin >> t;
    for (int T = 1; T <= t; T++) {
        cin >> a >> b >> s;

        n = a.length();
        m = b.length();

        F[0][0] = b[0] == s[0] ? 0 : 1;
        F[0][1] = a[0] == s[0] ? 0 : 1;
        for (int i = 2; i < n; i++) {
            F[0][i] = -1;
        }
        for (int l = 1; l < n + m; l++) {
            if (l < m) {
                LL c = b[l] == s[l] ? 0 : 1;
                F[l][0] = F[l - 1][0] + c;
            }
            else F[l][0] = -1;
            for (int i = 1; i <= l + 1; i++) {
                if (l - i > m || i > n) {
                    F[l][i] = -1;
                    continue;
                }
                
                if (l - i < 0) {
                    LL c2 = a[i - 1] == s[l] ? 0 : 1;
                    F[l][i] = F[l - 1][i - 1] + c2;
                }
                else if (i - 1 < 0) {
                    LL c1 = b[l - i] == s[l] ? 0 : 1;
                    F[l][i] = F[l - 1][i] + c1;
                }
                else {
                    LL c1 = b[l - i] == s[l] ? 0 : 1;
                    LL c2 = a[i - 1] == s[l] ? 0 : 1;

                    if (F[l - 1][i] == -1) F[l][i] = F[l - 1][i - 1] + c2;
                    else F[l][i] = min(F[l - 1][i] + c1, F[l - 1][i - 1] + c2);
                }

            }
            //for (int i = l + 2; i < n; i++) F[l][i] = -1;
        }
        cout << F[s.length() - 1][n] << endl;
    }


    return 0;
}