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

Previous 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();



}

Submission (verdict: Accepted):
#include <iostream>

#include <string>

#include <vector>

//#include <algorithm>

//#include<queue>

//#include<map>



using namespace std;



void alg()

{

    string a, b, c;

    

    cin >> a >> b >> c;



    vector<vector<int>> weight(a.length() + 1, vector<int>(b.length() + 1));



    weight[0][0] = 0;



    for (int k = 1; k <= c.length(); k++)

        for (int i = max(0, k - (int)b.length()); i <= k; i++)

        {

            if (i > a.length())

                break;



            int j = k - i;



            if (i == 0)

            {

                int add = 0;

            

                if (c[k-1] != b[j-1])

                    add = 1;



                weight[i][j] = weight[i][j - 1] + add;

            }

            else if (j == 0)

            {

                int add = 0;



                if (c[k-1] != a[i-1])

                    add = 1;



                weight[i][j] = weight[i - 1][j] + add;

            }

            else

            {

                int add_i = 0;

                int add_j = 0;



                if (c[k - 1] != b[j - 1])

                    add_j = 1;



                if (c[k - 1] != a[i - 1])

                    add_i = 1;



                weight[i][j] = min(weight[i - 1][j] + add_i, weight[i][j - 1] + add_j);

            }

                

                



        }



    cout << weight[a.length()][b.length()] << endl;

}



int main()

{

    int t;



    cin >> t;



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

        alg();



}