给定一个数组cost [] ,该数组包含将(a – z)中的每个字母相加的成本以及由小写英文字母(可能是也可能不是Panagram )组成的字符串str的成本。任务是通过以下操作使给定的字符串成为Panagram:
- 在str中添加一个字符的成本是与该字符关联的成本的两倍。
- 从str中删除一个字符将导致获得与该字符相关的确切成本。
打印使给定字符串成为Panagram的成本,如果收益大于成本,则打印0 。
例子:
Input: arr[] = {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},
str = “geeksforgeeks”
Output: 32
Occurrences of the characters are as follow:
g = 2, e = 4, k = 2, s = 2, f = 1, o = 1 and r = 1
The remaining 19 characters from the English alphabets will be added at cost 2 for each i.e. 2 * 19 = 38
And, 1, 3, 1 and 1 occurrences of the characters ‘g’, ‘e’, ‘k’ and ‘s’ respectively can be traded for gain (1 + 3 + 1 + 1) i.e. 6
So, the normalized cost is 38 – 6 = 32
Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26},
str = “thequickbrownfoxjumpsoverthelazydog”
Output: 0
The given string is already a Panagram
方法:
- 将每个字符的出现都存储在一个数组ences []中。
- 为每个字符ch初始化gain = 0并开始遍历数组的出现次数[] :
- 如果ch发生不止一次,例如说x次,则可以将其发生的x – 1换成一定的收益,即增益=增益+ cost [ch] *(x – 1) 。
- 如果CH不STR发生然后它在成本即增益=增益两次添加– (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