📜  通过用给定的备用数字替换数字段来最大化给定的数字

📅  最后修改于: 2021-04-27 17:28:41             🧑  作者: Mango

给定大量的N位数字。我们还提供了10个数字,它们代表从09的所有一位数字的备用数字。我们可以用给定的备用数字替换N中的任何数字,但只允许一次替换任何连续的数字段一次,任务是替换任何连续的数字段,以使获得的数字最大。所有可能的替代品。

例子:

方法:由于我们需要获得尽可能大的数字,因此请从左侧进行迭代,找到其替代数字大于当前数字的数字,并继续替换即将出现的数字,直到替代数字不小于该数字为止。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the maximized number
string get_maximum(string s, int a[])
{
    int n = s.size();
  
    // Iterate till the end of the string
    for (int i = 0; i < n; i++) {
  
        // Check if it is greater or not
        if (s[i] - '0' < a[s[i] - '0']) {
            int j = i;
  
            // Replace with the alternate till smaller
            while (j < n && (s[j] - '0' <= a[s[j] - '0'])) {
                s[j] = '0' + a[s[j] - '0'];
                j++;
            }
  
            return s;
        }
    }
  
    // Return original s in case
    // no change took place
    return s;
}
  
// Driver Code
int main()
{
    string s = "1337";
    int a[] = { 0, 1, 2, 5, 4, 6, 6, 3, 1, 9 };
    cout << get_maximum(s, a);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
// Function to return the maximized number
static String get_maximum(char[] s, int a[])
{
    int n = s.length;
  
    // Iterate till the end of the string
    for (int i = 0; i < n; i++)
    {
  
        // Check if it is greater or not
        if (s[i] - '0' < a[s[i] - '0']) 
        {
            int j = i;
  
            // Replace with the alternate till smaller
            while (j < n && (s[j] - '0' <= a[s[j] - '0'])) 
            {
                s[j] = (char) ('0' + a[s[j] - '0']);
                j++;
            }
  
            return String.valueOf(s);
        }
    }
  
    // Return original s in case
    // no change took place
    return String.valueOf(s);
}
  
// Driver Code
public static void main(String[] args)
{
    String s = "1337";
    int a[] = { 0, 1, 2, 5, 4, 6, 6, 3, 1, 9 };
    System.out.println(get_maximum(s.toCharArray(), a));
}
}
  
/* This code contributed by PrinciRaj1992 */


Python3
# Python3 implementation of the approach 
  
# Function to return the maximized number 
def get_maximum(s, a) : 
    s = list(s)
    n = len(s) 
      
    # Iterate till the end of the string 
    for i in range(n) :
          
        # Check if it is greater or not
        if (ord(s[i]) - ord('0') < a[ord(s[i]) - ord('0')]) :
            j = i
              
            # Replace with the alternate till smaller 
            while (j < n and (ord(s[j]) - ord('0') <=
                            a[ord(s[j]) - ord('0')])) :
                s[j] = chr(ord('0') + a[ord(s[j]) - ord('0')])
                j += 1
              
            return "".join(s);
      
    # Return original s in case 
    # no change took place 
    return s 
  
  
# Driver Code 
if __name__ == "__main__" :
      
    s = "1337"
    a = [ 0, 1, 2, 5, 4, 6, 6, 3, 1, 9 ] 
    print(get_maximum(s, a))
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
  
class GFG
{
  
// Function to return the maximized number
static String get_maximum(char[] s, int[] a)
{
    int n = s.Length;
  
    // Iterate till the end of the string
    for (int i = 0; i < n; i++)
    {
  
        // Check if it is greater or not
        if (s[i] - '0' < a[s[i] - '0']) 
        {
            int j = i;
  
            // Replace with the alternate till smaller
            while (j < n && (s[j] - '0' <= a[s[j] - '0'])) 
            {
                s[j] = (char) ('0' + a[s[j] - '0']);
                j++;
            }
  
            return String.Join("",s);
        }
    }
  
    // Return original s in case
    // no change took place
    return String.Join("",s);
}
  
// Driver Code
public static void Main(String[] args)
{
    String s = "1337";
    int[] a = { 0, 1, 2, 5, 4, 6, 6, 3, 1, 9 };
    Console.WriteLine(get_maximum(s.ToCharArray(), a));
}
}
  
// This code contributed by Rajput-Ji


PHP


输出:
1557