📌  相关文章
📜  查找具有相同的前半部分和后半部分总和的所有偶数长度的二进制序列

📅  最后修改于: 2021-04-24 05:41:59             🧑  作者: Mango

给定数字n,找到所有长度为2n的二进制序列,以使前n位的总和与后n位的总和相同。

例子:

Input:  N = 2
Output: 
0101 1111 1001 0110 0000 1010 

Input:  N = 3
Output:  
011011 001001 011101 010001 101011 111111
110011 101101 100001 110101 001010 011110 
010010 001100 000000 010100 101110 100010 
110110 100100 

这个想法是固定第一个和最后一个位,然后递归剩余的2 *(n-1)个位。修复首位和末位时,有四种可能性–

  1. 第一位和最后一位为1,两侧的其余n – 1位也应具有相同的总和。
  2. 第一位和最后一位为0,两侧剩余的n – 1位也应具有相同的总和。
  3. 第一位为1,最后一位为0,左侧剩余的n – 1位之和应比右侧的n-1位之和少1。
  4. 第一位为0,最后一位为1,左侧剩余的n – 1位之和应比右侧的n-1位之和多1。

以下是上述想法的实现–

C++
// C++ program to print even length binary sequences
// whose sum of first and second half bits is same
#include 
using namespace std;
  
// Function to print even length binary sequences
// whose sum of first and second half bits is same
  
// diff --> difference between sums of first n bits
// and last n bits
// out --> output array
// start --> starting index
// end --> ending index
void findAllSequences(int diff, char* out, int start, int end)
{
    // We can't cover difference of more than n with 2n bits
    if (abs(diff) > (end - start + 1) / 2)
        return;
  
    // if all bits are filled
    if (start > end)
    {
        // if sum of first n bits and last n bits are same
        if (diff == 0)
            cout << out << " ";
        return;
    }
  
    // fill first bit as 0 and last bit as 1
    out[start] = '0', out[end] = '1';
    findAllSequences(diff + 1, out, start + 1, end - 1);
  
    // fill first and last bits as 1
    out[start] = out[end] = '1';
    findAllSequences(diff, out, start + 1, end - 1);
  
    // fill first and last bits as 0
    out[start] = out[end] = '0';
    findAllSequences(diff, out, start + 1, end - 1);
  
    // fill first bit as 1 and last bit as 0
    out[start] = '1', out[end] = '0';
    findAllSequences(diff - 1, out, start + 1, end - 1);
}
  
// Driver program
int main()
{
    // input number
    int n = 2;
  
    // allocate string contaning 2*n characters
    char out[2 * n + 1];
  
    // null terminate output array
    out[2 * n] = '\0';
  
    findAllSequences(0, out, 0, 2*n - 1);
  
    return 0;
}


Java
// Java program to print even length binary 
// sequences whose sum of first and second
// half bits is same
import java.io.*;
import java.util.*;
  
class GFG 
{
    // Function to print even length binary sequences
    // whose sum of first and second half bits is same
   
    // diff --> difference between sums of first n bits
    // and last n bits
    // out --> output array
    // start --> starting index
    // end --> ending index
    static void findAllSequences(int diff, char out[], 
                                     int start, int end)
    {
        // We can't cover difference of more 
        // than n with 2n bits
        if (Math.abs(diff) > (end - start + 1) / 2)
            return;
   
        // if all bits are filled
        if (start > end)
        {
            // if sum of first n bits and
            // last n bits are same
            if (diff == 0)
            {
                System.out.print(out);
                System.out.print(" ");
            }    
            return;
        }
   
        // fill first bit as 0 and last bit as 1
        out[start] = '0';
        out[end] = '1';
        findAllSequences(diff + 1, out, start + 1, end - 1);
   
        // fill first and last bits as 1
        out[start] = out[end] = '1';
        findAllSequences(diff, out, start + 1, end - 1);
   
        // fill first and last bits as 0
        out[start] = out[end] = '0';
        findAllSequences(diff, out, start + 1, end - 1);
   
        // fill first bit as 1 and last bit as 0
        out[start] = '1';
        out[end] = '0';
        findAllSequences(diff - 1, out, start + 1, end - 1);
    }
      
    // Driver program
    public static void main (String[] args) 
    {
        // input number
        int n = 2;
   
        // allocate string contaning 2*n characters
        char[] out = new char[2 * n + 1];
   
        // null terminate output array
        out[2 * n] = '\0';
   
        findAllSequences(0, out, 0, 2*n - 1);
    }
}
  
// This code is contributed by Pramod Kumar


Python3
# Python3 program to print even length binary sequences
# whose sum of first and second half bits is same
  
# Function to print even length binary sequences
# whose sum of first and second half bits is same
  
# diff --> difference between sums of first n bits
# and last n bits
# out --> output array
# start --> starting index
# end --> ending index
def findAllSequences(diff, out, start, end):
  
    # We can't cover difference of more than n with 2n bits
    if (abs(diff) > (end - start + 1) // 2):
        return;
  
    # if all bits are filled
    if (start > end):
        # if sum of first n bits and last n bits are same
        if (diff == 0):
            print(''.join(list(out)),end=" ");
        return;
  
    # fill first bit as 0 and last bit as 1
    out[start] = '0';
    out[end] = '1';
    findAllSequences(diff + 1, out, start + 1, end - 1);
  
    # fill first and last bits as 1
    out[start] = out[end] = '1';
    findAllSequences(diff, out, start + 1, end - 1);
  
    # fill first and last bits as 0
    out[start] = out[end] = '0';
    findAllSequences(diff, out, start + 1, end - 1);
  
    # fill first bit as 1 and last bit as 0
    out[start] = '1';
    out[end] = '0';
    findAllSequences(diff - 1, out, start + 1, end - 1);
  
# Driver program
  
# input number
n = 2;
  
# allocate string contaning 2*n characters
out=[""]*(2*n);
  
findAllSequences(0, out, 0, 2*n - 1);
  
# This code is contributed by mits


C#
// C# program to print even length binary 
// sequences whose sum of first and second
// half bits is same
using System;
  
class GFG {
      
    // Function to print even length binary
    // sequences whose sum of first and 
    // second half bits is same
  
    // diff --> difference between sums of
    // first n bits
    // and last n bits
    // out --> output array
    // start --> starting index
    // end --> ending index
    static void findAllSequences(int diff,
            char []outt, int start, int end)
    {
          
        // We can't cover difference of 
        // more than n with 2n bits
        if (Math.Abs(diff) > (end - start
                                   + 1) / 2)
            return;
  
        // if all bits are filled
        if (start > end)
        {
              
            // if sum of first n bits and
            // last n bits are same
            if (diff == 0)
            {
                Console.Write(outt);
                Console.Write(" ");
            } 
            return;
        }
  
        // fill first bit as 0 and last bit
        // as 1
        outt[start] = '0';
        outt[end] = '1';
        findAllSequences(diff + 1, outt, 
                        start + 1, end - 1);
  
        // fill first and last bits as 1
        outt[start] = outt[end] = '1';
        findAllSequences(diff, outt, 
                        start + 1, end - 1);
  
        // fill first and last bits as 0
        outt[start] = outt[end] = '0';
        findAllSequences(diff, outt, 
                         start + 1, end - 1);
  
        // fill first bit as 1 and last 
        // bit as 0
        outt[start] = '1';
        outt[end] = '0';
        findAllSequences(diff - 1, outt,
                         start + 1, end - 1);
    }
      
    // Driver program
    public static void Main () 
    {
          
        // input number
        int n = 2;
  
        // allocate string contaning 2*n 
        // characters
        char []outt = new char[2 * n + 1];
  
        // null terminate output array
        outt[2 * n] = '\0';
  
        findAllSequences(0, outt, 0, 2*n - 1);
    }
}
  
// This code is contributed by nitin mittal.


PHP
 difference between sums of first n bits
// and last n bits
// out --> output array
// start --> starting index
// end --> ending index
function findAllSequences($diff, $out, $start, $end)
{
    // We can't cover difference of more than n with 2n bits
    if (abs($diff) > (int)(($end - $start + 1) / 2))
        return;
  
    // if all bits are filled
    if ($start > $end)
    {
        // if sum of first n bits and last n bits are same
        if ($diff == 0)
            print(implode("",$out)." ");
        return;
    }
  
    // fill first bit as 0 and last bit as 1
    $out[$start] = '0';
    $out[$end] = '1';
    findAllSequences($diff + 1, $out, $start + 1, $end - 1);
  
    // fill first and last bits as 1
    $out[$start] = $out[$end] = '1';
    findAllSequences($diff, $out, $start + 1, $end - 1);
  
    // fill first and last bits as 0
    $out[$start] = $out[$end] = '0';
    findAllSequences($diff, $out, $start + 1, $end - 1);
  
    // fill first bit as 1 and last bit as 0
    $out[$start] = '1';
    $out[$end] = '0';
    findAllSequences($diff - 1, $out, $start + 1, $end - 1);
}
  
// Driver program
  
    // input number
    $n = 2;
  
    // allocate string contaning 2*n characters
    $out=array_fill(0,2*$n,"");
  
    findAllSequences(0, $out, 0, 2*$n - 1);
  
// This code is contributed by chandan_jnu
?>


输出:

0101 1111 1001 0110 0000 1010