📌  相关文章
📜  分割整数的数字,使其满足给定条件

📅  最后修改于: 2021-04-22 03:34:43             🧑  作者: Mango

给定一个整数X ,任务是将其数字分为AB两组,这样,当排列A组的所有数字,然后按从左到右的顺序排列B组的所有数字时,数字的顺序不减小。它们出现在X中。如果没有这样的分区,则打印-1 ,否则返回与X长度相同的字符串S ,其中S [i]AB。

例子:

方法:

  1. 让我们假设一个数字D ,使得所有小于D的数字都进入组A,而所有大于D的数字都进入组B。
  2. 对于等于D的数字,仅当组B之前有数字出现时,它才会进入组A ,否则它将进入组B。
  3. 在这样的划分之后,检查它是否形成一个非递减序列。否则,请尝试其他一些D。
  4. D的取值范围是0〜9。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to generate sequence
// from the given string
vector makeSeq(string s, int a[])
{
    // Initialize vector to
    // store sequence
    vector seq;
  
    // First add all the digits
    // of group A from left to right
    for (int i = 0; i < s.size(); i++)
        if (s[i] == 'A')
            seq.push_back(a[i]);
  
    // Then add all the digits
    // of group B from left to right
    for (int i = 0; i < s.size(); i++)
        if (s[i] == 'B')
            seq.push_back(a[i]);
  
    // Return the sequence
    return seq;
}
  
// Function that returns true if
// the sequence is non-decreasing
bool checkSeq(vector v)
{
    // Initialize result
    bool check = true;
  
    for (int i = 1; i < v.size(); i++)
        if (v[i] < v[i - 1])
            check = false;
  
    return check;
}
  
// Function to partition the digits
// of an integer such that it satisfies
// the given conditions
string digitPartition(int X)
{
    // Convert the integer to string
    string num = to_string(X);
  
    // Length of the string
    int l = num.size();
  
    // Array to store the digits
    int a[l];
  
    // Storing the digits of X in array
    for (int i = 0; i < l; i++)
        a[i] = (num[i] - '0');
  
    for (int D = 0; D < 10; D++) {
  
        // Initialize the result
        string res = "";
  
        // Loop through the digits
        for (int i = 0; i < l; i++) {
  
            // Put into group A if
            // digit less than D
            if (a[i] < D)
                res += 'A';
  
            // Put into group B if
            // digit greater than D
            else if (a[i] > D)
                res += 'B';
  
            // Put into group C if
            // digit equal to D
            else
                res += 'C';
        }
  
        bool flag = false;
  
        // Loop through the digits
        // to decide for group C digits
        for (int i = 0; i < l; i++) {
  
            // Set flag equal to true
            // if group B digit present
            if (res[i] == 'B')
                flag = true;
  
            // If flag is true put in
            // group A or else put in B
            if (res[i] == 'C')
                res[i] = flag ? 'A' : 'B';
        }
  
        // Generate the sequence from partition
        vector seq = makeSeq(res, a);
  
        // Check if the sequence is
        // non decreasing
        if (checkSeq(seq))
            return res;
    }
  
    // Return -1 if no such
    // partition is possible
    return "-1";
}
  
// Driver code
int main()
{
    int X = 777147777;
  
    cout << digitPartition(X);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
  
// Function to generate sequence
// from the given String
static Vector makeSeq(String s, int a[])
{
    // Initialize vector to
    // store sequence
    Vector seq = new Vector();
  
    // First add all the digits
    // of group A from left to right
    for (int i = 0; i < s.length(); i++)
        if (s.charAt(i) == 'A')
            seq.add(a[i]);
  
    // Then add all the digits
    // of group B from left to right
    for (int i = 0; i < s.length(); i++)
        if (s.charAt(i) == 'B')
            seq.add(a[i]);
  
    // Return the sequence
    return seq;
}
  
// Function that returns true if
// the sequence is non-decreasing
static boolean checkSeq(Vector v)
{
    // Initialize result
    boolean check = true;
  
    for (int i = 1; i < v.size(); i++)
        if (v.get(i) < v.get(i - 1))
            check = false;
  
    return check;
}
  
// Function to partition the digits
// of an integer such that it satisfies
// the given conditions
static String digitPartition(int X)
{
    // Convert the integer to String
    String num = String.valueOf(X);
  
    // Length of the String
    int l = num.length();
  
    // Array to store the digits
    int []a = new int[l];
  
    // Storing the digits of X in array
    for (int i = 0; i < l; i++)
        a[i] = (num.charAt(i) - '0');
  
    for (int D = 0; D < 10; D++) 
    {
  
        // Initialize the result
        String res = "";
  
        // Loop through the digits
        for (int i = 0; i < l; i++) 
        {
  
            // Put into group A if
            // digit less than D
            if (a[i] < D)
                res += 'A';
  
            // Put into group B if
            // digit greater than D
            else if (a[i] > D)
                res += 'B';
  
            // Put into group C if
            // digit equal to D
            else
                res += 'C';
        }
  
        boolean flag = false;
  
        // Loop through the digits
        // to decide for group C digits
        for (int i = 0; i < l; i++) 
        {
  
            // Set flag equal to true
            // if group B digit present
            if (res.charAt(i) == 'B')
                flag = true;
  
            // If flag is true put in
            // group A or else put in B
            if (res.charAt(i) == 'C')
                res = res.substring(0, i) + 
                (flag ? 'A' : 'B') + res.substring(i + 1);
        }
  
        // Generate the sequence from partition
        Vector seq = makeSeq(res, a);
  
        // Check if the sequence is
        // non decreasing
        if (checkSeq(seq))
            return res;
    }
  
    // Return -1 if no such
    // partition is possible
    return "-1";
}
  
// Driver code
public static void main(String[] args)
{
    int X = 777147777;
  
    System.out.print(digitPartition(X));
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach 
  
# Function to generate sequence 
# from the given string 
def makeSeq(s, a) : 
  
    # Initialize vector to 
    # store sequence 
    seq = []; 
  
    # First add all the digits 
    # of group A from left to right 
    for i in range(len(s)) :
        if (s[i] == 'A') :
            seq.append(a[i]); 
  
    # Then add all the digits 
    # of group B from left to right 
    for i in range(len(s)) :
        if (s[i] == 'B') :
            seq.append(a[i]); 
  
    # Return the sequence 
    return seq; 
  
# Function that returns true if 
# the sequence is non-decreasing 
def checkSeq(v) : 
  
    # Initialize result 
    check = True; 
  
    for i in range(1, len(v)) :
        if (v[i] < v[i - 1]) :
            check = False; 
  
    return check; 
  
# Function to partition the digits 
# of an integer such that it satisfies 
# the given conditions 
def digitPartition(X) : 
      
    # Convert the integer to string 
    num = str(X); 
  
    # Length of the string 
    l = len(num); 
  
    # Array to store the digits 
    a = [0]*l; 
  
    # Storing the digits of X in array 
    for i in range(l) : 
        a[i] = (ord(num[i]) - ord('0')); 
  
    for D in range(10) :
  
        # Initialize the result 
        res = ""; 
  
        # Loop through the digits 
        for i in range(l) : 
  
            # Put into group A if 
            # digit less than D 
            if (a[i] < D) :
                res += 'A'; 
  
            # Put into group B if 
            # digit greater than D 
            elif (a[i] > D) :
                res += 'B'; 
  
            # Put into group C if 
            # digit equal to D 
            else :
                res += 'C'; 
  
        flag = False; 
  
        # Loop through the digits 
        # to decide for group C digits 
        for i in range(l) :
  
            # Set flag equal to true 
            # if group B digit present 
            if (res[i] == 'B') :
                flag = True; 
  
            # If flag is true put in 
            # group A or else put in B 
            res = list(res);
              
            if (res[i] == 'C') :
                res[i] = 'A' if flag else 'B'; 
  
        # Generate the sequence from partition 
        seq = makeSeq(res, a); 
  
        # Check if the sequence is 
        # non decreasing 
        if (checkSeq(seq)) :
            return "".join(res); 
  
    # Return -1 if no such 
    # partition is possible 
    return "-1"; 
  
# Driver code 
if __name__ == "__main__" : 
  
    X = 777147777; 
  
    print(digitPartition(X)); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG
{
  
// Function to generate sequence
// from the given String
static List makeSeq(String s, int []a)
{
    // Initialize vector to
    // store sequence
    List seq = new List();
  
    // First add all the digits
    // of group A from left to right
    for (int i = 0; i < s.Length; i++)
        if (s[i] == 'A')
            seq.Add(a[i]);
  
    // Then add all the digits
    // of group B from left to right
    for (int i = 0; i < s.Length; i++)
        if (s[i] == 'B')
            seq.Add(a[i]);
  
    // Return the sequence
    return seq;
}
  
// Function that returns true if
// the sequence is non-decreasing
static bool checkSeq(List v)
{
    // Initialize result
    bool check = true;
  
    for (int i = 1; i < v.Count; i++)
        if (v[i] < v[i - 1])
            check = false;
  
    return check;
}
  
// Function to partition the digits
// of an integer such that it satisfies
// the given conditions
static String digitPartition(int X)
{
    // Convert the integer to String
    String num = String.Join("",X);
  
    // Length of the String
    int l = num.Length;
  
    // Array to store the digits
    int []a = new int[l];
  
    // Storing the digits of X in array
    for (int i = 0; i < l; i++)
        a[i] = (num[i] - '0');
  
    for (int D = 0; D < 10; D++) 
    {
  
        // Initialize the result
        String res = "";
  
        // Loop through the digits
        for (int i = 0; i < l; i++) 
        {
  
            // Put into group A if
            // digit less than D
            if (a[i] < D)
                res += 'A';
  
            // Put into group B if
            // digit greater than D
            else if (a[i] > D)
                res += 'B';
  
            // Put into group C if
            // digit equal to D
            else
                res += 'C';
        }
  
        bool flag = false;
  
        // Loop through the digits
        // to decide for group C digits
        for (int i = 0; i < l; i++) 
        {
  
            // Set flag equal to true
            // if group B digit present
            if (res[i] == 'B')
                flag = true;
  
            // If flag is true put in
            // group A or else put in B
            if (res[i] == 'C')
                res = res.Substring(0, i) + 
                (flag ? 'A' : 'B') + res.Substring(i + 1);
        }
  
        // Generate the sequence from partition
        List seq = makeSeq(res, a);
  
        // Check if the sequence is
        // non decreasing
        if (checkSeq(seq))
            return res;
    }
  
    // Return -1 if no such
    // partition is possible
    return "-1";
}
  
// Driver code
public static void Main(String[] args)
{
    int X = 777147777;
  
    Console.Write(digitPartition(X));
}
}
  
// This code is contributed by Rajput-Ji


输出:
BBBAABBBB