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

Previous submission (verdict: Wrong answer on test 2):
#include <iostream>

//#include <vector>

//#include <algorithm>

#include <string>

//#include<queue>

//#include<map>



using namespace std;



bool is_good(string& a, string& b, string& c, int num_of_wild, int pos_a, int pos_b)

{

    if (pos_a + pos_b == c.length())

        return true;



    // можно ли добавить а

    if (pos_a < a.length() and c[pos_a + pos_b] == a[pos_a])

    {

        if (is_good(a, b, c, num_of_wild, pos_a + 1, pos_b))

            return true;

    }



    // можно ли добавить b

    if (pos_b < b.length() and c[pos_a + pos_b] == b[pos_b])

    {

        if (is_good(a, b, c, num_of_wild, pos_a, pos_b + 1))

            return true;

    }



    // можно ли добавить wild card

    if (num_of_wild > 0)

    {

        if (is_good(a, b, c, num_of_wild - 1, pos_a + 1, pos_b))

            return true;



        if (is_good(a, b, c, num_of_wild - 1, pos_a, pos_b + 1))

            return true;

    }



    return false;

}



void alg()

{

    string a, b, c;



    cin >> a >> b >> c;



    int start = 0;

    int end = c.length();



    if (is_good(a, b, c, start, 0, 0))

    {

        cout << 0 << endl;

        return;

    }



    while (end - start != 1)

    {

        int middle = (start + end) / 2;



        if (is_good(a, b, c, middle, 0, 0))

        {

            end = middle;

        }

        else

        {

            start = middle;

        }

    }



    cout << end << endl;

}



int main()

{

    int t;



    cin >> t;



    for (int i = 0; i < t; i++)

        alg();



}

Submission (verdict: Time limit exceeded on test 7):
#include <iostream>

//#include <vector>

//#include <algorithm>

#include <string>

//#include<queue>

//#include<map>



using namespace std;



bool is_good(string& a, string& b, string& c, int num_of_wild, int pos_a, int pos_b)

{

    if (pos_a + pos_b == c.length())

        return true;



    // можно ли добавить а

    if (pos_a < a.length() and c[pos_a + pos_b] == a[pos_a])

    {

        if (is_good(a, b, c, num_of_wild, pos_a + 1, pos_b))

            return true;

    }



    // можно ли добавить b

    if (pos_b < b.length() and c[pos_a + pos_b] == b[pos_b])

    {

        if (is_good(a, b, c, num_of_wild, pos_a, pos_b + 1))

            return true;

    }



    // можно ли добавить wild card

    if (num_of_wild > 0)

    {

        if (pos_a < a.length() and is_good(a, b, c, num_of_wild - 1, pos_a + 1, pos_b))

            return true;



        if (pos_b < b.length() and is_good(a, b, c, num_of_wild - 1, pos_a, pos_b + 1))

            return true;

    }



    return false;

}



void alg()

{

    string a, b, c;



    cin >> a >> b >> c;



    int start = 0;

    int end = c.length();



    if (is_good(a, b, c, start, 0, 0))

    {

        cout << 0 << endl;

        return;

    }



    while (end - start != 1)

    {

        int middle = (start + end) / 2;



        if (is_good(a, b, c, middle, 0, 0))

        {

            end = middle;

        }

        else

        {

            start = middle;

        }

    }



    cout << end << endl;

}



int main()

{

    int t;



    cin >> t;



    for (int i = 0; i < t; i++)

        alg();



}