📜  通过从长度为2K的回文子字符串中删除K长度前缀,可以将字符串的词典排列最小排列为长度K

📅  最后修改于: 2021-04-29 04:07:13             🧑  作者: Mango

给定一个长度为N的二进制字符串str和一个整数K ,任务是找到字符串str在字典上最小的排列,可以通过从长度为2K的回文子字符串中删除每个K长度前缀来减小其长度为K。如果不存在这样的排列,则打印“不可能”。

例子:

方法:请按照以下步骤解决问题:

  • 计算字符串出现的0秒和1秒的数量。如果0 s或1 s的计数是N / K的倍数则可以减小字符串的K。否则,打印“不可能”。
  • 初始化字符串tempStr,以存储最初形成的按字典顺序最小的2K长度的字符串。
  • 初始化字符串finalStr以存储满足给定条件的结果字符串。
  • 根据以下公式将0 s分配为长度为2K的段:
    • 否在2K长度串零的,N0 =((以N的长度字符串的零数目)的字符串的/(总长度))*(2K)
    • 2K长度的子串中没有1,N1 =(2 * K – 2K长度的子串中没有0)
  • tempStr附加0s N0 / 2次和1s N1 / 2次,再附加0s N0 / 2次。
  • 追加tempStr(N / 2K)次以finalStr和刀片(N%2K)tempStr的字符在finalStr的末尾。
  • 打印finalStr作为结果字符串。

下面是上述方法的实现:

C++14
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the number of
// zeroes present in the string
int count_zeroes(int n, string str)
{
    int cnt = 0;
 
    // Traverse the string
    for (int i = 0; i < str.size(); i++) {
        if (str[i] == '0')
            cnt++;
    }
 
    // Return the count
    return cnt;
}
 
// Function to rearrange the string s.t
// the string can be reduced to a length
// K as per the given rules
string kReducingStringUtil(
    int n, int k, string str,
    int no_of_zeroes)
{
 
    // Distribute the count of 0s and
    // 1s in segment of length 2k
    int zeroes_in_2k = ((no_of_zeroes)
                        * (2 * k))
                       / n;
 
    int ones_in_2k = 2 * k - zeroes_in_2k;
 
    // Store string that are initially
    // have formed lexicographically
    // smallest 2k length substring
    string temp_str = "";
 
    for (int i = 0;
         i < (zeroes_in_2k) / 2; i++) {
        temp_str.push_back('0');
    }
    for (int i = 0; i < ones_in_2k; i++) {
        temp_str.push_back('1');
    }
    for (int i = 0;
         i < (zeroes_in_2k) / 2; i++) {
        temp_str.push_back('0');
    }
 
    // Store the lexicographically
    // smallest string of length n
    // that satisfy the conditon
    string final_str = "";
 
    // Insert temp_str into final_str
    // (n/2k) times and add (n%2k)
    // characters of temp_str at end
    for (int i = 0;
         i < n / (2 * k); i++) {
        final_str += (temp_str);
    }
 
    for (int i = 0;
         i < n % (2 * k); i++) {
        final_str.push_back(temp_str[i]);
    }
 
    // Return the final string
    return final_str;
}
 
// Function to reduce the string to
// length K that follows the given
// conditions
string kReducingString(int n, int k,
                       string str)
{
    // If the string contains either
    // 0s or 1s then it always be
    // reduced into a K length string
    int no_of_zeroes = count_zeroes(n, str);
 
    int no_of_ones = n - no_of_zeroes;
 
    // If the string contains only 0s
    // 1s then it always reduces to
    // a K length string
    if (no_of_zeroes == 0
        || no_of_zeroes == n) {
        return str;
    }
 
    // If K = 1
    if (k == 1) {
        if (no_of_zeroes == 0
            || no_of_zeroes == n) {
            return str;
        }
        else {
            return "Not Possible";
        }
    }
 
    // Check whether the given string
    // is K reducing string or not
    bool check = 0;
 
    for (int i = (n / k);
         i < n; i += (n / k)) {
 
        if (no_of_zeroes == i
            || no_of_ones == i) {
            check = 1;
            break;
        }
    }
 
    if (check == 0) {
        return "Not Possible";
    }
 
    // Otherwise recursively find
    // the string
    return kReducingStringUtil(n, k,
                               str,
                               no_of_zeroes);
}
 
// Driver Code
int main()
{
    string str = "0000100001100001";
    int K = 4;
    int N = str.length();
 
    // Function Call
    cout << kReducingString(N, K, str);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to count the number of
// zeroes present in the string
static int count_zeroes(int n, String str)
{
    int cnt = 0;
     
    // Traverse the string
    for(int i = 0; i < str.length(); i++)
    {
        if (str.charAt(i) == '0')
            cnt++;
    }
 
    // Return the count
    return cnt;
}
 
// Function to rearrange the string s.t
// the string can be reduced to a length
// K as per the given rules
static String kReducingStringUtil(int n, int k,
                                  String str,
                                  int no_of_zeroes)
{
     
    // Distribute the count of 0s and
    // 1s in segment of length 2k
    int zeroes_in_2k = ((no_of_zeroes) *
                        (2 * k)) / n;
 
    int ones_in_2k = 2 * k - zeroes_in_2k;
 
    // Store string that are initially
    // have formed lexicographically
    // smallest 2k length substring
    String temp_str = "";
 
    for(int i = 0; i < (zeroes_in_2k) / 2; i++)
    {
        temp_str += '0';
    }
    for(int i = 0; i < ones_in_2k; i++)
    {
        temp_str += '1';
    }
    for(int i = 0; i < (zeroes_in_2k) / 2; i++)
    {
        temp_str += '0';
    }
     
    // Store the lexicographically
    // smallest string of length n
    // that satisfy the conditon
    String final_str = "";
 
    // Insert temp_str into final_str
    // (n/2k) times and add (n%2k)
    // characters of temp_str at end
    for(int i = 0; i < n / (2 * k); i++)
    {
        final_str += (temp_str);
    }
 
    for(int i = 0; i < n % (2 * k); i++)
    {
        final_str += temp_str.charAt(i);
    }
 
    // Return the final string
    return final_str;
}
 
// Function to reduce the string to
// length K that follows the given
// conditions
static String kReducingString(int n, int k,
                              String str)
{
     
    // If the string contains either
    // 0s or 1s then it always be
    // reduced into a K length string
    int no_of_zeroes = count_zeroes(n, str);
 
    int no_of_ones = n - no_of_zeroes;
 
    // If the string contains only 0s
    // 1s then it always reduces to
    // a K length string
    if (no_of_zeroes == 0 ||
        no_of_zeroes == n)
    {
        return str;
    }
 
    // If K = 1
    if (k == 1)
    {
        if (no_of_zeroes == 0 ||
            no_of_zeroes == n)
        {
            return str;
        }
        else
        {
            return "Not Possible";
        }
    }
 
    // Check whether the given string
    // is K reducing string or not
    boolean check = false;
 
    for(int i = (n / k); i < n; i += (n / k))
    {
        if (no_of_zeroes == i ||
            no_of_ones == i)
        {
            check = true;
            break;
        }
    }
 
    if (check == false)
    {
        return "Not Possible";
    }
 
    // Otherwise recursively find
    // the string
    return kReducingStringUtil(n, k, str,
                               no_of_zeroes);
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "0000100001100001";
    int K = 4;
    int N = str.length();
 
    // Function Call
    System.out.println(kReducingString(N, K, str));
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program for the above approach
 
# Function to count the number of
# zeroes present in the string
def count_zeroes(n, str):
   
  cnt = 0
 
  # Traverse the string
  for i in range(0, len(str)):
    if (str[i] == '0'):
      cnt += 1
       
  # Return the count
  return cnt
 
# Function to rearrange the string s.t
# the string can be reduced to a length
# K as per the given rules
def kReducingStringUtil(n, k, str,
                        no_of_zeroes):
   
  # Distribute the count of 0s and
  # 1s in segment of length 2k
  zeroes_in_2k = (((no_of_zeroes) *
                   (2 * k)) // n)
 
  ones_in_2k = 2 * k - zeroes_in_2k
 
  # Store string that are initially
  # have formed lexicographically
  # smallest 2k length substring
  temp_str = ""
 
  for i in range(0, (zeroes_in_2k) // 2):
    temp_str += '0'
     
  for i in range(0, (ones_in_2k)):
    temp_str += '1'
   
  for i in range(0, (zeroes_in_2k) // 2):
    temp_str += '0'
     
  # Store the lexicographically
  # smallest string of length n
  # that satisfy the conditon
  final_str = ""
 
  # Insert temp_str into final_str
  # (n/2k) times and add (n%2k)
  # characters of temp_str at end
  for i in range(0, n // (2 * k)):
    final_str += (temp_str)
 
  for i in range(0, n % (2 * k)):
    final_str += (temp_str[i])
     
  # Return the final string
  return final_str
 
# Function to reduce the string to
# length K that follows the given
# conditions
def kReducingString(n, k, str):
   
  # If the string contains either
  # 0s or 1s then it always be
  # reduced into a K length string
  no_of_zeroes = count_zeroes(n, str)
 
  no_of_ones = n - no_of_zeroes
 
  # If the string contains only 0s
  # 1s then it always reduces to
  # a K length string
  if (no_of_zeroes == 0 or
      no_of_zeroes == n):
    return str
 
  # If K = 1
  if (k == 1):
    if (no_of_zeroes == 0 or
        no_of_zeroes == n):
      return str
    else:
      return "Not Possible"
     
  # Check whether the given string
  # is K reducing string or not
  check = 0
 
  for i in range((n // k), n, (n // k)):
    if (no_of_zeroes == i or no_of_ones == i):
      check = 1
      break
       
  if (check == 0):
    return "Not Possible"
 
  # Otherwise recursively find
  # the string
  return kReducingStringUtil(n, k, str,
                             no_of_zeroes)
 
# Driver Code
if __name__ == '__main__':
   
  str = "0000100001100001"
  K = 4;
  N = len(str)
 
  # Function Call
  print(kReducingString(N, K, str))
   
# This code is contributed by akhilsaini


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count the number of
// zeroes present in the string
static int count_zeroes(int n, string str)
{
    int cnt = 0;
 
    // Traverse the string
    for(int i = 0; i < str.Length; i++)
    {
        if (str[i] == '0')
            cnt++;
    }
 
    // Return the count
    return cnt;
}
 
// Function to rearrange the string s.t
// the string can be reduced to a length
// K as per the given rules
static string kReducingStringUtil(int n, int k,
                                  string str,
                                  int no_of_zeroes)
{
     
    // Distribute the count of 0s and
    // 1s in segment of length 2k
    int zeroes_in_2k = ((no_of_zeroes) *
                        (2 * k)) / n;
 
    int ones_in_2k = 2 * k - zeroes_in_2k;
 
    // Store string that are initially
    // have formed lexicographically
    // smallest 2k length substring
    string temp_str = "";
 
    for(int i = 0; i < (zeroes_in_2k) / 2; i++)
    {
        temp_str += '0';
    }
    for(int i = 0; i < ones_in_2k; i++)
    {
        temp_str += '1';
    }
    for(int i = 0; i < (zeroes_in_2k) / 2; i++)
    {
        temp_str += '0';
    }
 
    // Store the lexicographically
    // smallest string of length n
    // that satisfy the conditon
    string final_str = "";
 
    // Insert temp_str into final_str
    // (n/2k) times and add (n%2k)
    // characters of temp_str at end
    for(int i = 0; i < n / (2 * k); i++)
    {
        final_str += (temp_str);
    }
 
    for(int i = 0; i < n % (2 * k); i++)
    {
        final_str += temp_str[i];
    }
 
    // Return the final string
    return final_str;
}
 
// Function to reduce the string to
// length K that follows the given
// conditions
static string kReducingString(int n, int k,
                              string str)
{
     
    // If the string contains either
    // 0s or 1s then it always be
    // reduced into a K length string
    int no_of_zeroes = count_zeroes(n, str);
 
    int no_of_ones = n - no_of_zeroes;
 
    // If the string contains only 0s
    // 1s then it always reduces to
    // a K length string
    if (no_of_zeroes == 0 ||
        no_of_zeroes == n)
    {
        return str;
    }
 
    // If K = 1
    if (k == 1)
    {
        if (no_of_zeroes == 0 ||
            no_of_zeroes == n)
        {
            return str;
        }
        else
        {
            return "Not Possible";
        }
    }
 
    // Check whether the given string
    // is K reducing string or not
    bool check = false;
 
    for(int i = (n / k); i < n; i += (n / k))
    {
        if (no_of_zeroes == i ||
            no_of_ones == i)
        {
            check = true;
            break;
        }
    }
 
    if (check == false)
    {
        return "Not Possible";
    }
 
    // Otherwise recursively find
    // the string
    return kReducingStringUtil(n, k, str,
                               no_of_zeroes);
}
 
// Driver Code
public static void Main()
{
    string str = "0000100001100001";
    int K = 4;
    int N = str.Length;
 
    // Function Call
    Console.WriteLine(kReducingString(N, K, str));
}
}
 
// This code is contributed by akhilsaini


输出:
0001100000011000




时间复杂度: O(N)
辅助空间: O(N)