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

Previous submission (verdict: Time limit exceeded on test 5):
#include <bits/stdc++.h>
using namespace std;
// #define int long long int

#define MOD 10000007
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vector<int>>
#define mii map<int, int>
#define si set<int>
#define sc set<char>
#define ff first
#define ss second

void printVector(vector <int> v){
    for(auto i : v) cout << i << " ";
    cout << endl;
}
void printCharVector(vector <char> v){
    for(auto i : v) cout << i << " ";
    cout << endl;
}

void printStringVector(vector <string> v){
    for(auto i : v) cout << i << " ";
    cout << endl;
}

void print2DVector(vector <vector<int>> v){
    for( auto row : v){
        for (auto i : row)
            cout << i << " ";
        cout <<  endl;
    }
}

void printMap(map <int,int> m){
    for (auto x : m) {
        cout << x.first << " : " << x.second << endl;
    }
}

void printPairVector(vector <pair<int,int>> v){
    for(auto i : v) cout << i.first << " "<< i.second << endl;
}

bool sortByFirst(pair<int,int> a,pair<int,int> b){return a.first < b.first;}
bool sortBySecond(pair<int,int> a,pair<int,int> b){return a.second < b.second;}


int HCF(int a,int b){
    while(b!=0){
        int temp = a%b;
        a = b;
        b = temp;    
    }
    return a;
}

int eqn_gcd(int a, int b, int &x, int &y){
    if(b==0){x=1;y=0;return a;}
    int x1, y1;
    int g = eqn_gcd(b,a%b,x1,y1);
    x = y1; y = x1 - (a/b)*y1;
    return g; 
    // x = x0 - (b/g)*m
    // y = y0 + (a/g)*m
}

bool prime_calculator(int n){
    for(int i=2;i*i<=n;i++)if(n%i==0) return 1;
    return 0;
}

bool comparator(pair<int,int> a,pair<int,int> b){return a.first < b.first;}

int power_bin(int x,int y){
    x = x%MOD;
    int res = 1;
    if(!x)return 0;
    while(y>0){
        if(y&1) res = (res*x)%MOD;
        y = y>>1;
        x = (x*x)%MOD;
    }
    return res;
}

void yes(){ cout << "YES" << endl;}
void no(){ cout << "NO" << endl;}

int findMSBPosition(int num) {
    if (num == 0) return -1;  // No MSB for zero
    return 31 - __builtin_clz(num);  // Position is 0-indexed
}

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
 
/* procedure to find the least two elements in an array
min1 = min2 = LLONG_MAX;
    for(k=0;k<m;k++){
        cin >> a;
        if(a<=min1){
            min2 = min1;
            min1 = a;
        }
        else if(a<min2) min2 = a;
    }
*/
/*sorting in desceding order
    std::sort(v.begin(), v.end(), greater<int>());
*/

/* defining priority queue with least number on top
auto compare = [](int a, int b) { return a > b; };
    std::priority_queue<int, std::vector<int>, decltype(compare)> pq(compare);

or you can pair each element of the vectors in another vector with basis vector as first
element and then apply sort function.
*/

/*sorting one vector with respect to other (ascending) use it only for indices
    for(int i=0;i<n;i++) {
        cin >> k;
        a[i].first = k;
        a[i].second = i;
    }
    sort(a.begin(),a.end(),sortByFirst);

*/

/* finding if a given substring is present in a string
s.find(substring,start_pos); #starts from start_pos

slicing of a string
s.substr(start_pos,len);

frequency of an element in a string
int occurences  = count(s.begin(),s.end(),element);
*/


int dp[4000][2000];

void solve(){
    int n,m,k,i,j,ans=0;
    string a,b,c;
    cin >> a >> b >> c;
    n = c.length();
    m = a.length();
    k = b.length();
    
    memset(dp,-1,sizeof(dp));

    function<int(int,int)> rec = [&] (int i, int p)->int{
        // cout << "//" << i << " " << n<< endl;
        if(i==n) return 0;
        if(dp[i][p]!=-1) return dp[i][p];
        int q = i-p;
        int ans = INT_MAX;
        if(p<m){
            if(c[i]==a[p]) ans = min(ans,rec(i+1,p+1));
            else ans = min(ans,rec(i+1,p+1)+1);
        }
        if(q<k){
            if(c[i]==b[q]) ans = min(ans,rec(i+1,p));  
            else ans = min(ans,rec(i+1,p)+1);
        }
        
        dp[i][p] = ans;
        return ans;
    };
    
    int res = rec(0,0);
    cout << res << endl;
}


int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // #ifndef ONLINE_JUDGE
    // freopen("input.txt","r",stdin);
    // #endif
    
    int T;
    cin >> T;
    
    while(T--) 
        solve();

    return 0;
}   



Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
// #define int long long int

#define MOD 10000007
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vector<int>>
#define mii map<int, int>
#define si set<int>
#define sc set<char>
#define ff first
#define ss second

void printVector(vector <int> v){
    for(auto i : v) cout << i << " ";
    cout << endl;
}
void printCharVector(vector <char> v){
    for(auto i : v) cout << i << " ";
    cout << endl;
}

void printStringVector(vector <string> v){
    for(auto i : v) cout << i << " ";
    cout << endl;
}

void print2DVector(vector <vector<int>> v){
    for( auto row : v){
        for (auto i : row)
            cout << i << " ";
        cout <<  endl;
    }
}

void printMap(map <int,int> m){
    for (auto x : m) {
        cout << x.first << " : " << x.second << endl;
    }
}

void printPairVector(vector <pair<int,int>> v){
    for(auto i : v) cout << i.first << " "<< i.second << endl;
}

bool sortByFirst(pair<int,int> a,pair<int,int> b){return a.first < b.first;}
bool sortBySecond(pair<int,int> a,pair<int,int> b){return a.second < b.second;}


int HCF(int a,int b){
    while(b!=0){
        int temp = a%b;
        a = b;
        b = temp;    
    }
    return a;
}

int eqn_gcd(int a, int b, int &x, int &y){
    if(b==0){x=1;y=0;return a;}
    int x1, y1;
    int g = eqn_gcd(b,a%b,x1,y1);
    x = y1; y = x1 - (a/b)*y1;
    return g; 
    // x = x0 - (b/g)*m
    // y = y0 + (a/g)*m
}

bool prime_calculator(int n){
    for(int i=2;i*i<=n;i++)if(n%i==0) return 1;
    return 0;
}

bool comparator(pair<int,int> a,pair<int,int> b){return a.first < b.first;}

int power_bin(int x,int y){
    x = x%MOD;
    int res = 1;
    if(!x)return 0;
    while(y>0){
        if(y&1) res = (res*x)%MOD;
        y = y>>1;
        x = (x*x)%MOD;
    }
    return res;
}

void yes(){ cout << "YES" << endl;}
void no(){ cout << "NO" << endl;}

int findMSBPosition(int num) {
    if (num == 0) return -1;  // No MSB for zero
    return 31 - __builtin_clz(num);  // Position is 0-indexed
}

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
 
/* procedure to find the least two elements in an array
min1 = min2 = LLONG_MAX;
    for(k=0;k<m;k++){
        cin >> a;
        if(a<=min1){
            min2 = min1;
            min1 = a;
        }
        else if(a<min2) min2 = a;
    }
*/
/*sorting in desceding order
    std::sort(v.begin(), v.end(), greater<int>());
*/

/* defining priority queue with least number on top
auto compare = [](int a, int b) { return a > b; };
    std::priority_queue<int, std::vector<int>, decltype(compare)> pq(compare);

or you can pair each element of the vectors in another vector with basis vector as first
element and then apply sort function.
*/

/*sorting one vector with respect to other (ascending) use it only for indices
    for(int i=0;i<n;i++) {
        cin >> k;
        a[i].first = k;
        a[i].second = i;
    }
    sort(a.begin(),a.end(),sortByFirst);

*/

/* finding if a given substring is present in a string
s.find(substring,start_pos); #starts from start_pos

slicing of a string
s.substr(start_pos,len);

frequency of an element in a string
int occurences  = count(s.begin(),s.end(),element);
*/

string a,b,c;
int n,m,k;
 
int dp[4000][2000];
int rec(int i, int p){
        // cout << "//" << i << " " << n<< endl;
        if(i==n) return 0;
        if(dp[i][p]!=-1) return dp[i][p];
        int q = i-p;
        int ans = INT_MAX;
        if(p<m){
            if(c[i]==a[p]) ans = min(ans,rec(i+1,p+1));
            else ans = min(ans,rec(i+1,p+1)+1);
        }
        if(q<k){
            if(c[i]==b[q]) ans = min(ans,rec(i+1,p));  
            else ans = min(ans,rec(i+1,p)+1);
        }
        
        dp[i][p] = ans;
        return ans;
};

void solve(){
    int i,j,ans=0;
    cin >> a >> b >> c;
    n = c.length();
    m = a.length();
    k = b.length();
    
    memset(dp,-1,sizeof(dp));

    
    
    int res = rec(0,0);
    cout << res << endl;
}


int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // #ifndef ONLINE_JUDGE
    // freopen("input.txt","r",stdin);
    // #endif
    
    int T;
    cin >> T;
    
    while(T--) 
        solve();

    return 0;
}