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

📅  最后修改于: 2021-10-26 06:35:23             🧑  作者: 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


Javascript


输出:
1557

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程