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, 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;
    }
}

Submission (verdict: Accepted):
#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, map<pair<int, int>, int> & used){    
    if(elem >= c.size()){
        return ans;
    }
    pair<int, int> look;
    look.first = ina;
    look.second = inb;
    if(used.count(look) == true){
        return used[look]+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, used);
        look.first++;
        used[look] = res1-ans;
        int res2 = recurrencia(a, b, c, elem, ans, ina, inb+1, used);
        look.second++;
        look.first--;
        used[look] = res2-ans;
        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, used);
        look.first++;
        used[look] = res1-ans;
        int res2 = recurrencia(a, b, c, elem, ans, ina, inb+1, used);
        look.second++;
        look.first--;
        used[look] = res2-ans;
        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, used);
        look.first++;
        used[look] = res1-ans;
        int res2 = recurrencia(a, b, c, elem, ans+1, ina, inb+1, used);
        look.second++;
        look.first--;
        used[look] = res2-(ans+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, used);
        look.first++;
        used[look] = res1-(ans+1);
        int res2 = recurrencia(a, b, c, elem, ans, ina, inb+1, used);
        look.second++;
        look.first--;
        used[look] = res2-ans;
        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;
        map<pair<int, int>, int> used;
        cout << recurrencia(a, b, c, elem, ans, ina, inb, used) << endl;
    }
}