📌  相关文章
📜  具有给定频率的连续字符对和的二进制字符串

📅  最后修改于: 2021-04-22 07:08:57             🧑  作者: Mango

给定三个整数PQR ,任务是生成一个具有P,Q和R对的连续字符对的二进制字符串,分别具有和0、1和2。
例子:

方法:为了解决此问题,我们需要遵循以下步骤:

  • 如果PR不为零,则至少存在一对和为1的连续字符。因此,如果提供的Q为0,则在这种情况下无法形成这样的字符串。因此,返回-1
  • 如果Q是零,只有P的一个和R是非零的,则附加0 P + 1,如果P是非零或附加1个R +如果R是非零1周时间。
  • 如果它们都不为零:
    • 分别追加01 P +1Q +1次。
    • Q – 1交替追加01

下面是上述方法的实现:

C++
// C++ program to generate a binary
// string with given frequencies
// of sums of consecutive
// pair of characters
 
#include 
using namespace std;
 
// A Function that generates
// and returns the binary string
string build_binary_str(int p,
                        int q, int r)
{
 
    // P: Frequency of consecutive
    // characters with sum 0
    // Q: Frequency of consecutive
    // characters with sum 1
    // R: Frequency of consecutive
    // characters with sum 2
 
    string ans = "";
 
    // If no consecutive
    // character adds up to 1
    if (q == 0) {
 
        // Not possible if both P
        // and Q are non - zero
        if (p != 0 && r != 0) {
            return "-1";
        }
 
        else {
 
            // If P is not equal to 0
            if (p) {
 
                // Append 0 P + 1 times
                ans = string(p + 1, '0');
            }
            else {
                // Append 1 R + 1 times
                ans = string(r + 1, '1');
            }
        }
    }
    else {
 
        // Append "01" to satisfy Q
        for (int i = 1; i <= q + 1; i++) {
            if (i % 2 == 0) {
                ans += '0';
            }
            else {
                ans += '1';
            }
        }
 
        // Append "0" P times
        ans.insert(1, string(p, '0'));
 
        // Append "1" R times
        ans.insert(0, string(r, '1'));
    }
    return ans;
}
 
// Driver Code
int main()
{
    int p = 1, q = 2, r = 2;
    cout << build_binary_str(p, q, r);
    return 0;
}


Java
// Java program to generate a binary
// String with given frequencies
// of sums of consecutive
// pair of characters
 class GFG{
  
// A Function that generates
// and returns the binary String
static String build_binary_str(int p,
                               int q, int r)
{
  
    // P: Frequency of consecutive
    // characters with sum 0
    // Q: Frequency of consecutive
    // characters with sum 1
    // R: Frequency of consecutive
    // characters with sum 2
    String ans = "";
  
    // If no consecutive
    // character adds up to 1
    if (q == 0)
    {
  
        // Not possible if both P
        // and Q are non - zero
        if (p != 0 && r != 0)
        {
            return "-1";
        }
  
        else
        {
           
            // If P is not equal to 0
            if (p > 0)
            {
  
                // Append 0 P + 1 times
                ans = Strings(p + 1, '0');
            }
            else
            {
                // Append 1 R + 1 times
                ans = Strings(r + 1, '1');
            }
        }
    }
    else
    {
  
        // Append "01" to satisfy Q
        for (int i = 1; i <= q + 1; i++)
        {
            if (i % 2 == 0)
            {
                ans += '0';
            }
            else
            {
                ans += '1';
            }
        }
  
        // Append "0" P times
        ans = ans.substring(0, 1) + Strings(p, '0') + ans.substring(1);
  
        // Append "1" R times
        ans = Strings(r, '1') + ans;
    }
    return ans;
}
  
static String Strings(int p, char c)
{
    String ans = "";
    for (int i = 0; i < p; i++)
        ans += c;
    return ans;
}
  
// Driver Code
public static void main(String[] args)
{
    int p = 1, q = 2, r = 2;
    System.out.print(build_binary_str(p, q, r));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program to generate a binary
# string with given frequencies
# of sums of consecutive
# pair of characters
 
# A Function that generates
# and returns the binary string
def build_binary_str(p, q, r):
     
    # P: Frequency of consecutive
    # characters with sum 0
    # Q: Frequency of consecutive
    # characters with sum 1
    # R: Frequency of consecutive
    # characters with sum 2
    ans = ""
 
    # If no consecutive
    # character adds up to 1
    if (q == 0):
         
        # Not possible if both P
        # and Q are non - zero
        if (p != 0 and r != 0):
            return "-1"
 
        else:
             
            # If P is not equal to 0
            if (p):
                 
                # Append 0 P + 1 times
                temp = ""
                for i in range(p + 1):
                    temp += '0'
                     
                ans = temp
                 
            else:
                 
                # Append 1 R + 1 times
                temp = ""
                for i in range(r + 1):
                    temp += '1'
                     
                ans = temp
 
    else:
         
        # Append "01" to satisfy Q
        for i in range(1, q + 2):
            if (i % 2 == 0):
                ans += '0'
            else:
                ans += '1'
 
        # Append "0" P times
        temp = ""
        for i in range(p):
            temp += '0'
             
        st = ""
        st += ans[0]
        st += temp
         
        for i in range(1, len(ans)):
            st += ans[i]
             
        ans = st
 
        # Append "1" R times
        temp = ""
        for i in range(r):
            temp += '1'
 
        ans = temp + ans
         
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    p = 1
    q = 2
    r = 2
     
    print(build_binary_str(p, q, r))
 
# This code is contributed by Surendra_Gangwar


C#
// C# program to generate a binary
// String with given frequencies
// of sums of consecutive
// pair of characters
using System;
class GFG{
  
// A Function that generates
// and returns the binary String
static String build_binary_str(int p,
                               int q, int r)
{
  // P: Frequency of consecutive
  // characters with sum 0
  // Q: Frequency of consecutive
  // characters with sum 1
  // R: Frequency of consecutive
  // characters with sum 2
  String ans = "";
 
  // If no consecutive
  // character adds up to 1
  if (q == 0)
  {
    // Not possible if both P
    // and Q are non - zero
    if (p != 0 && r != 0)
    {
      return "-1";
    }
 
    else
    {         
      // If P is not equal to 0
      if (p > 0)
      {
        // Append 0 P + 1 times
        ans = Strings(p + 1, '0');
      }
      else
      {
        // Append 1 R + 1 times
        ans = Strings(r + 1, '1');
      }
    }
  }
  else
  {
    // Append "01" to satisfy Q
    for (int i = 1; i <= q + 1; i++)
    {
      if (i % 2 == 0)
      {
        ans += '0';
      }
      else
      {
        ans += '1';
      }
    }
 
    // Append "0" P times
    ans = ans.Substring(0, 1) +
          Strings(p, '0') +
          ans.Substring(1);
 
    // Append "1" R times
    ans = Strings(r, '1') + ans;
  }
  return ans;
}
 
  static String Strings(int p, char c)
  {
    String ans = "";
    for (int i = 0; i < p; i++)
      ans += c;
    return ans;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int p = 1, q = 2, r = 2;
    Console.Write(build_binary_str(p,
                                   q, r));
  }
}
 
// This code is contributed by 29AjayKumar


输出:
111001





时间复杂度: O(P + Q + R)