📜  制作不含子序列的字符串的最低成本

📅  最后修改于: 2021-04-24 03:50:10             🧑  作者: Mango

给定一个字符串str ,该字符串由小写英文字母和一个长度相同的正整数arr []组成。任务是从给定的字符串,使得字符串没有子序列形成的字符串“代码”除去一些字符。删除字符str [i]的成本为arr [i] 。找到实现目标的最低成本。

例子:

方法:如果可以使用带有“代码”的任何子序列,则需要删除单个字符。删除每个字符的成本在arr []中给出。因此,遍历字符串,对于code的每个字符,计算除去它们的成本。最后,去除所有字符的成本中的最低要求是最低成本。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the minimum cost
int findCost(string str, int arr[], int n)
{
    long long costofC = 0, costofO = 0,
              costofD = 0, costofE = 0;
  
    // Traverse the string
    for (int i = 0; i < n; i++) {
  
        // Min Cost to remove 'c'
        if (str[i] == 'c')
            costofC += arr[i];
  
        // Min Cost to remove subsequence "co"
        else if (str[i] == 'o')
            costofO = min(costofC, costofO + arr[i]);
  
        // Min Cost to remove subsequence "cod"
        else if (str[i] == 'd')
            costofD = min(costofO, costofD + arr[i]);
  
        // Min Cost to remove subsequence "code"
        else if (str[i] == 'e')
            costofE = min(costofD, costofE + arr[i]);
    }
  
    // Return the minimum cost
    return costofE;
}
  
// Driver program
int main()
{
    string str = "geekcodergeeks";
    int arr[] = { 1, 2, 1, 3, 4, 2, 6, 4, 6, 2, 3, 3, 3, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findCost(str, arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
  
class GFG {
  
    // Function to return the minimum cost
    static int findCost(String str, int arr[], int n)
    {
        long costofC = 0, costofO = 0,
             costofD = 0, costofE = 0;
  
        // Traverse the string
        for (int i = 0; i < n; i++) {
  
            // Min Cost to remove 'c'
            if (str.charAt(i) == 'c')
                costofC += arr[i];
  
            // Min Cost to remove subsequence "co"
            else if (str.charAt(i) == 'o')
                costofO = Math.min(costofC, costofO + arr[i]);
  
            // Min Cost to remove subsequence "cod"
            else if (str.charAt(i) == 'd')
                costofD = Math.min(costofO, costofD + arr[i]);
  
            // Min Cost to remove subsequence "code"
            else if (str.charAt(i) == 'e')
                costofE = Math.min(costofD, costofE + arr[i]);
        }
  
        // Return the minimum cost
        return (int)costofE;
    }
  
    // Driver program
    public static void main(String[] args)
    {
        String str = "geekcodergeeks";
        int arr[] = { 1, 2, 1, 3, 4, 2, 6, 4, 6, 2, 3, 3, 3, 2 };
        int n = arr.length;
        System.out.print(findCost(str, arr, n));
    }
}
// This code has been contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
  
# Function to return the minimum cost
def findCost(str, arr, n):
    costofC, costofO = 0, 0
    costofD, costofE = 0, 0
  
    # Traverse the string
    for i in range(n):
  
        # Min Cost to remove 'c'
        if (str[i] == 'c'):
            costofC += arr[i]
  
        # Min Cost to remove subsequence "co"
        elif (str[i] == 'o'):
            costofO = min(costofC, costofO + arr[i])
  
        # Min Cost to remove subsequence "cod"
        elif (str[i] == 'd'):
            costofD = min(costofO, costofD + arr[i])
  
        # Min Cost to remove subsequence "code"
        elif (str[i] == 'e'):
            costofE = min(costofD, costofE + arr[i])
  
    # Return the minimum cost
    return costofE
  
# Driver Code
if __name__ == '__main__':
    str = "geekcodergeeks"
    arr = [1, 2, 1, 3, 4, 2, 6, 4, 6, 2, 3, 3, 3, 2]
    n = len(arr)
    print(findCost(str, arr, n))
  
# This code contributed by PrinciRaj1992


C#
// C# implementation of the approach
using System;
  
class GFG
{
  
    // Function to return the minimum cost
    public static int findCost(string str, 
                            int[] arr, int n)
    {
        long costofC = 0, costofO = 0, 
              costofD = 0, costofE = 0;
  
        // Traverse the string
        for (int i = 0; i < n; i++)
        {
  
            // Min Cost to remove 'c'
            if (str[i] == 'c')
            {
                costofC += arr[i];
            }
  
            // Min Cost to remove subsequence "co"
            else if (str[i] == 'o')
            {
                costofO = Math.Min(costofC, costofO + arr[i]);
            }
  
            // Min Cost to remove subsequence "cod"
            else if (str[i] == 'd')
            {
                costofD = Math.Min(costofO, costofD + arr[i]);
            }
  
            // Min Cost to remove subsequence "code"
            else if (str[i] == 'e')
            {
                costofE = Math.Min(costofD, costofE + arr[i]);
            }
        }
  
        // Return the minimum cost
        return (int)costofE;
    }
  
    // Driver program
    public static void Main(string[] args)
    {
        string str = "geekcodergeeks";
        int[] arr = new int[] {1, 2, 1, 3, 4, 2, 6, 
                                4, 6, 2, 3, 3, 3, 2};
        int n = arr.Length;
        Console.Write(findCost(str, arr, n));
    }
}
  
// This code is contributed by shrikanth13


输出:
2