📜  制作字符串的费用套装2

📅  最后修改于: 2021-04-26 18:26:55             🧑  作者: Mango

给定一个数组cost [] ,该数组包含将(a – z)中的每个字母相加的成本以及由小写英文字母(可能是也可能不是Panagram )组成的字符串str的成本。任务是通过以下操作使给定的字符串成为Panagram:

  1. str中添加一个字符的成本是与该字符关联的成本的两倍。
  2. str中删除一个字符将导致获得与该字符相关的确切成本。

打印使给定字符串成为Panagram的成本,如果收益大于成本,则打印0

例子:

方法:

  • 将每个字符的出现都存储在一个数组ences []中
  • 为每个字符ch初始化gain = 0并开始遍历数组的出现次数[]
    • 如果ch发生不止一次,例如说x次,则可以将其发生的x – 1换成一定的收益,即增益=增益+ cost [ch] *(x – 1)
    • 如果CHSTR发生然后它在成本即增益=增益两次添加– (2 *成本[CH])。
  • 如果增益≥0,则打印0
  • 否则,打印增益* -1就是制作str的成本。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
using namespace std;
  
// Function to return the cost
// to make str a Panagram
int costToPanagram(string str, int cost[])
{
  
    int i, n = str.length();
    int occurrences[26] = {0};
  
    // Count the occurrences of 
    // each lowercase character
    for (i = 0; i < n; i++)
        occurrences[str[i] - 'a']++;
  
    // To store the total gain
    int gain = 0;
    for (i = 0; i < 26; i++) 
    {
  
        // If some character is missing, 
        // it has to be added at twice the cost
        if (occurrences[i] == 0)
            gain -= (2 * cost[i]);
  
        // If some character appears more 
        // than once, all of its occurrences 
        // except 1 can be traded for some gain
        else if (occurrences[i] > 1)
            gain += (cost[i] * (occurrences[i] - 1));
    }
  
    // If gain is more than the cost
    if (gain >= 0)
        return 0;
  
    // Return the total cost if gain < 0
    return (gain * -1);
}
  
// Driver code
int main()
{
    int cost[] = { 1, 1, 1, 1, 1, 1, 1, 1, 
                   1, 1, 1, 1, 1, 1, 1, 1, 
                   1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
    string str = "geeksforgeeks";
  
    cout << costToPanagram(str, cost);
}
  
// This code is contributed by
// Surendra_Gangwar


Java
// Java implementation of the approach
public class GFG {
  
    // Function to return the cost to make str a Panagram
    static int costToPanagram(String str, int cost[])
    {
  
        int i, n = str.length();
        int occurrences[] = new int[26];
  
        // Count the occurrences of each lowercase character
        for (i = 0; i < n; i++)
            occurrences[str.charAt(i) - 'a']++;
  
        // To store the total gain
        int gain = 0;
        for (i = 0; i < 26; i++) {
  
            // If some character is missing, it has to be added
            // at twice the cost
            if (occurrences[i] == 0)
                gain -= (2 * cost[i]);
  
            // If some character appears more than once
            // all of its occurrences except 1
            // can be traded for some gain
            else if (occurrences[i] > 1)
                gain += (cost[i] * (occurrences[i] - 1));
        }
  
        // If gain is more than the cost
        if (gain >= 0)
            return 0;
  
        // Return the total cost if gain < 0
        return (gain * -1);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int cost[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
        String str = "geeksforgeeks";
  
        System.out.println(costToPanagram(str, cost));
    }
}


Python3
# Python3 implementation of the approach 
      
# Function to return the cost to 
# make string a Panagram 
def costToPanagram(string, cost): 
  
    n = len(string) 
    occurrences = [0] * 26
  
    # Count the occurrences of each 
    # lowercase character 
    for i in range(n):
        occurrences[ord(string[i]) - ord('a')] += 1
  
    # To store the total gain 
    gain = 0
    for i in range(26):
  
        # If some character is missing,
        # it has to be added at twice the cost 
        if occurrences[i] == 0: 
            gain -= 2 * cost[i] 
  
        # If some character appears more than 
        # once all of its occurrences except 1 
        # can be traded for some gain 
        elif occurrences[i] > 1: 
            gain += cost[i] * (occurrences[i] - 1) 
  
    # If gain is more than the cost 
    if gain >= 0: 
        return 0
  
    # Return the total cost if gain < 0 
    return gain * -1
  
# Driver code 
if __name__ == "__main__":
      
    cost = [1, 1, 1, 1, 1, 1, 1, 
               1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 
               1, 1, 1, 1, 1, 1] 
      
    string = "geeksforgeeks"
  
    print(costToPanagram(string, cost)) 
      
# This code is contributed
# by Rituraj Jain


C#
// C# implementation of the approach 
using System;
  
class GFG 
{ 
  
    // Function to return the cost to make str a Panagram 
    static int costToPanagram(string str, int []cost) 
    { 
  
        int i, n = str.Length ;
        int []occurrences = new int[26]; 
  
        // Count the occurrences of each lowercase character 
        for (i = 0; i < n; i++) 
            occurrences[str[i] - 'a']++; 
  
        // To store the total gain 
        int gain = 0; 
        for (i = 0; i < 26; i++)
        { 
  
            // If some character is missing, it has to be added 
            // at twice the cost 
            if (occurrences[i] == 0) 
                gain -= (2 * cost[i]); 
  
            // If some character appears more than once 
            // all of its occurrences except 1 
            // can be traded for some gain 
            else if (occurrences[i] > 1) 
                gain += (cost[i] * (occurrences[i] - 1)); 
        } 
  
        // If gain is more than the cost 
        if (gain >= 0) 
            return 0; 
  
        // Return the total cost if gain < 0 
        return (gain * -1); 
    } 
  
    // Driver code 
    public static void Main() 
    { 
        int []cost = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; 
        string str = "geeksforgeeks"; 
        Console.WriteLine(costToPanagram(str, cost)); 
    } 
} 
  
// This code is contributed by Ryuga


输出:
32