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

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

using namespace std;

#define int long long

int recurrencia(string a, string b, string & c, int elem, int ans, bool nec, bool esa){
    if(elem >= c.size()){
        return ans;
    }
    if(nec == true){
        if(esa == true)
            a.pop_back();
        else
            b.pop_back();
        nec = false;
    }
    if(a.size() == 0 && b.size() == 0){
        return ans;
    }
    if(a.size() == 0){
        for(int j = b.size()-1; j>=0; j--){
            if(b[j] != c[elem])
                ans++;
            elem++;
        }
        return ans;
    }
    else if(b.size() == 0){
        for(int j = a.size()-1; j>=0; j--){
            if(a[j] != c[elem])
                ans++;
            elem++;
        }
        return ans;
    }
    else if(c[elem] != a[a.size()-1] && c[elem] != b[b.size()-1]){
        ans++;
        esa = true;
        nec = true;
        int res1 = recurrencia(a, b, c, elem+1, ans, nec, esa);
        esa = false;
        int res2 = recurrencia(a, b, c, elem+1, ans, nec, esa);
        if(res1 < res2)
            ans = res1;
        else
            ans = res2;
        nec = false;
    }
    else if(c[elem] == a[a.size()-1] && c[elem] == b[b.size()-1]){
        esa = true;
        nec = true;
        int res1 = recurrencia(a, b, c, elem+1, ans, nec, esa);
        esa = false;
        int res2 = recurrencia(a, b, c, elem+1, ans, nec, esa);
        if(res1 < res2)
            ans = res1;
        else
            ans = res2;
        nec = false;
    }
    else if(c[elem] == a[a.size()-1]){
        nec = true;
        esa = true;
        int res1 = recurrencia(a, b, c, elem+1, ans, nec, esa);
        esa = false;
        int res2 = recurrencia(a, b, c, elem+1, ans+1, nec, esa);
        if(res1 < res2)
            ans = res1;
        else
            ans = res2;
        nec = false;
    }
    else if(c[elem] == b[b.size()-1]){
        esa = true;
        nec = true;
        int res1 = recurrencia(a, b, c, elem+1, ans+1, nec, esa);
        esa = false;
        int res2 = recurrencia(a, b, c, elem+1, ans, nec, esa);
        if(res1 < res2)
            ans = res1;
        else
            ans = res2;
        nec = false;
    }

    return ans;
}

signed main(){
    int t; cin >> t;
    for(int k = 0; k<t; k++){
        string conta, contb, c; cin >> conta >> contb >> c;
        string a;
        string b;
        for(int i = conta.size()-1; i>=0; i--){
            a += conta[i];
        }
        for(int i = contb.size()-1; i>=0; i--){
            b += contb[i];
        }
        int ans = 0;
        int elem = 0;
        bool nec = false;
        bool esa = false;
        cout << recurrencia(a, b, c, elem, ans, nec, esa) << endl;
    }
}

Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
 
using namespace std;
 
#define int long long
 
int recurrencia(string & a, string & b, string & c, int & elem, int ans, int ina, int inb){
    if(elem >= c.size()){
        return ans;
    }
    if(ina == a.size() && b.size() == inb){
        return ans;
    }
    if(ina == a.size()){
        for(int j = inb; j < b.size(); j++){
            if(b[j] != c[elem])
                ans++;
            elem++;
        }
        return ans;
    }
    else if(b.size() == inb){
        for(int j = ina; j<a.size(); j++){
            if(a[j] != c[elem])
                ans++;
            elem++;
        }
        return ans;
    }
    else if(c[elem] != a[ina] && c[elem] != b[inb]){
        elem++;
        ans++;
        int res1 = recurrencia(a, b, c, elem, ans, ina+1, inb);
        int res2 = recurrencia(a, b, c, elem, ans, ina, inb+1);
        if(res1 < res2)
            ans = res1;
        else
            ans = res2;
    }
    else if(c[elem] == a[ina] && c[elem] == b[inb]){
        elem++;
        int res1 = recurrencia(a, b, c, elem, ans, ina+1, inb);
        int res2 = recurrencia(a, b, c, elem, ans, ina, inb+1);
        if(res1 < res2)
            ans = res1;
        else
            ans = res2;
    }
    else if(c[elem] == a[ina]){
        elem++;
        int res1 = recurrencia(a, b, c, elem, ans, ina+1, inb);
        int res2 = recurrencia(a, b, c, elem, ans+1, ina, inb+1);
        if(res1 < res2)
            ans = res1;
        else
            ans = res2;
    }
    else if(c[elem] == b[inb]){
        elem++;
        int res1 = recurrencia(a, b, c, elem, ans+1, ina+1, inb);
        int res2 = recurrencia(a, b, c, elem, ans, ina, inb+1);
        if(res1 < res2)
            ans = res1;
        else
            ans = res2;
    }
    return ans;
}
 
signed main(){
    int t; cin >> t;
    for(int k = 0; k<t; k++){
        string a, b, c; cin >> a >> b >> c;
        int ans = 0;
        int elem = 0;
        int ina = 0;
        int inb = 0;
        cout << recurrencia(a, b, c, elem, ans, ina, inb) << endl;
    }
}