📌  相关文章
📜  对于给定的附加字符代价,按字典顺序的最大字符串可能

📅  最后修改于: 2021-05-24 20:09:01             🧑  作者: Mango

给定一个整数W和一个大小为26的数组a [] ,其中a i表示使用第i字母的代价,任务是从字典上找到可以为代价W生成的最大字符串。

例子:

方法:想法是将数组a []从字符“ z ”迭代为“ a ”字母。现在,在a []中找到总和等于W的组合。相同的重复数目可以从一个[]无限的次数来选择。然后,使用递归和回溯来解决该问题。请按照以下步骤解决问题:

  • 为在任一时刻的子问题总和= 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)