News Leaflets
A leading news portal.

Minimium cost to delete characters from String A to remove any subsequence as String B

0 52

  

#include <bits/stdc++.h>

using namespace std;

  

int dp[101][101][2];

  

int minCostUtil(string& a, int n,

                string& b, int m,

                vector<int>& c, int removed)

{

  

    

    if (n == 0 || m == 0) {

  

        

        

        if (removed == 0)

            return 99999;

        return 0;

    }

  

    

    if (dp[n][m][removed > 0 ? 1 : 0] != -1)

        return dp[n][m][removed > 0 ? 1 : 0];

  

    

    

    

    

    

    

    if (a[n - 1] == b[m - 1]) {

        dp[n][m][removed > 0 ? 1 : 0]

            = min(c[n - 1]

                      + minCostUtil(a, n - 1,

                                    b, m, c, removed),

                  minCostUtil(a, n - 1, b, m - 1,

                              c, removed - 1));

        return dp[n][m][removed > 0 ? 1 : 0];

    }

  

    

    

    

    

    else

        return dp[n][m][removed > 0 ? 1 : 0]

               = minCostUtil(a, n - 1,

                             b, m, c, removed);

}

  

int minCost(string& a, string& b,

            vector<int>& c)

{

    memset(dp, -1, sizeof(dp));

    return minCostUtil(a, a.size(), b,

                       b.size(), c, b.size());

}

  

int main()

{

    string A = "abccdabccdabccd";

    string B = "bccd";

    vector<int> arr = { 1, 2, 3, 4, 5,

                        6, 7, 8, 9, 10,

                        11, 12, 13, 14, 15 };

  

    cout << minCost(A, B, arr);

    return 0;

}

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! News Leaflets is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment