给定一个整数W和一个大小为 26 的数组a[] ,其中 a i表示使用第 i个字母表的成本,任务是按字典顺序找到可以为成本W生成的最大字符串。
例子:
Input: W = 236, a[] = {1, 1, 2, 33, 4, 6, 9, 7, 36, 32, 58, 32, 28, 904, 22, 255, 47, 69, 558, 544, 21, 36, 48, 85, 48, 58}
Output: zzzze
Explanation:
4 * (cost of z) + cost of e = 4 * 58 + 4 = 232 + 4 = 236
Input: W = 6674, a[] = {252, 288, 578, 746, 295, 884, 198, 655, 503, 868, 942, 506, 718, 498, 727, 338, 43, 768, 783, 312, 369, 712, 230, 106, 102, 554}
Output: zzzzzzzzzzzyyyyqqqq
Explanation:
11 * (cost of z) + 4 * (cost of y) + 4 * (cost of q) = 11 * 554 + 4 * 102 + 4 * 43 = 6094 + 408 + 172 = 6674
方法:这个想法是遍历数组a[]从字符’ z ‘ 到 ‘ a ‘ 字母。现在,在a[] 中找到总和等于W 的组合。可以从a[] 中无限次选择相同的重复数字。然后,使用递归和回溯来解决问题。请按照以下步骤解决问题:
- 为在任一时刻的子问题总和= 0,打印为存储在数组中到现在总结于W和字符的成本获得的字符串,因为已经产生字符串追加从“Z”开始的字符,由此形成的字符串是字典序最大的字符串。
- 否则,如果总和为负或索引 < 0,则为这些子问题返回 false。
- 否则,发现字典顺序最大的字符串由包括和不包括得到的字符串在当前字符可能。
- 按字典序打印完成上述步骤得到的最大字符串。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to find the
// lexicographically largest
// String possible
bool lexi_largest_String(int a[], int i,
int sum, vector &v)
{
// If sum is less than 0
if (sum < 0)
return false;
// If sum is equal to 0
if (sum == 0)
return true;
// If sum is less than 0
if (i < 0)
return false;
// Add current character
v.push_back(i);
// Check if selecting current
// character generates
// lexicographically largest String
if (lexi_largest_String(a, i,
sum - a[i], v))
return true;
// Backtrack if solution
// not found
auto it = v.end();
it--;
v.erase(it);
// Find the lexicographically
// largest String excluding
// the current character
return lexi_largest_String(a, i - 1,
sum, v);
}
// Function to print the
// lexicographically largest
// String generated
void generateString(int a[], int sum)
{
vector v;
// Function call
lexi_largest_String(a, 25,
sum, v);
// Stores the String
string s = "";
for (int j = 0; j < v.size(); j++)
s += (char)(v[j] + 'a');
// Print the lexicographically
// largest String formed
cout << s << endl;
}
// Driver code
int main()
{
// Cost of adding each
// alphabet
int a[] = {1, 1, 2, 33, 4, 6, 9,
7, 36, 32, 58, 32, 28,
904, 22, 255, 47, 69,
558, 544, 21, 36, 48,
85, 48, 58};
// Cost of generating
// the String
int sum = 236;
generateString(a, sum);
return 0;
}
// This code is contributed by divyeshrabadiya07
Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG{
// Function to find the
// lexicographically largest
// String possible
static boolean lexi_largest_String(int a[], int i,
int sum,
Vector v)
{
// If sum is less than 0
if (sum < 0)
return false;
// If sum is equal to 0
if (sum == 0)
return true;
// If sum is less than 0
if (i < 0)
return false;
// Add current character
v.add(i);
// Check if selecting current
// character generates
// lexicographically largest String
if (lexi_largest_String(a, i, sum -
a[i], v))
return true;
// Backtrack if solution
// not found
v.remove(v.size() - 1);
// Find the lexicographically
// largest String excluding
// the current character
return lexi_largest_String(a, i - 1,
sum, v);
}
// Function to print the
// lexicographically largest
// String generated
static void generateString(int a[],
int sum)
{
Vector v = new Vector();
// Function call
lexi_largest_String(a, 25, sum, v);
// Stores the String
String s = "";
for (int j = 0; j < v.size(); j++)
s += (char)(v.get(j) + 'a');
// Print the lexicographically
// largest String formed
System.out.print(s + "\n");
}
// Driver code
public static void main(String[] args)
{
// Cost of adding each alphabet
int a[] = {1, 1, 2, 33, 4, 6, 9,
7, 36, 32, 58, 32, 28,
904, 22, 255, 47, 69,
558, 544, 21, 36, 48,
85, 48, 58};
// Cost of generating
// the String
int sum = 236;
generateString(a, sum);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement
# the above appraoch
# Function to find the lexicographically
# largest string possible
def lexi_largest_string(a, i, sum, v):
# If sum is less than 0
if (sum < 0):
return False
# If sum is equal to 0
if (sum == 0):
return True
# If sum is less than 0
if (i < 0):
return False
# Add current character
v.append(i)
# Check if selecting current character
# generates lexicographically
# largest string
if(lexi_largest_string(a, i,
sum - a[i], v)):
return True
# Backtrack if solution not found
v.pop(-1)
# Find the lexicographically
# largest string excluding
# the current character
return lexi_largest_string(a, i - 1,
sum, v)
# Function to print the lexicographically
# largest string generated
def generateString(a, sum):
v = []
# Function call
lexi_largest_string(a, 25, sum , v)
# Stores the string
s = ""
for j in range(len(v)):
s += chr(v[j] + ord('a'))
# Print the lexicographically
# largest string formed
print(s)
# Driver code
if __name__ == '__main__':
a = [ 1, 1, 2, 33, 4, 6, 9,
7, 36, 32, 58, 32, 28,
904, 22, 255, 47, 69,
558, 544, 21, 36, 48,
85, 48, 58 ]
# Cost of generating
# the string
sum = 236
generateString(a, sum)
# This code is contributed by Shivam Singh
C#
// C# Program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the
// lexicographically largest
// String possible
static bool lexi_largest_String(int []a, int i,
int sum, List v)
{
// If sum is less than 0
if (sum < 0)
return false;
// If sum is equal to 0
if (sum == 0)
return true;
// If sum is less than 0
if (i < 0)
return false;
// Add current character
v.Add(i);
// Check if selecting current
// character generates
// lexicographically largest String
if (lexi_largest_String(a, i, sum -
a[i], v))
return true;
// Backtrack if solution
// not found
v.RemoveAt(v.Count - 1);
// Find the lexicographically
// largest String excluding
// the current character
return lexi_largest_String(a, i - 1,
sum, v);
}
// Function to print the
// lexicographically largest
// String generated
static void generateString(int []a,
int sum)
{
List v = new List();
// Function call
lexi_largest_String(a, 25, sum, v);
// Stores the String
String s = "";
for (int j = 0; j < v.Count; j++)
s += (char)(v[j] + 'a');
// Print the lexicographically
// largest String formed
Console.Write(s + "\n");
}
// Driver code
public static void Main(String[] args)
{
// Cost of adding each alphabet
int []a = {1, 1, 2, 33, 4, 6, 9,
7, 36, 32, 58, 32, 28,
904, 22, 255, 47, 69,
558, 544, 21, 36, 48,
85, 48, 58};
// Cost of generating
// the String
int sum = 236;
generateString(a, sum);
}
}
// This code is contributed by 29AjayKumar
zzzze
时间复杂度: O(2 26)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live