📌  相关文章
📜  分割二进制数以使每个部分都可被2整除的方式的数量

📅  最后修改于: 2021-04-24 14:31:03             🧑  作者: Mango

给定一个二进制字符串S ,任务是找到将其拆分为多个部分的方式,以使每个部分都可以被2整除。

例子:

方法:一种观察是,字符串只能在0之后进行分割。因此,计算字符串的零个数。我们将此计数称为c_zero 。假设字符串为偶数,除了最右边的字符串外,对于每个0 ,都有两种选择,即要么将字符串切成零,要么不将其切掉。因此,偶数字符串的最终答案变为2 (c_zero – 1)
字符串不能以1结束的情况。因此,对于奇数字符串,答案将始终为零,因为最后分割的部分将始终为奇数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
#define maxN 20
#define maxM 64
  
// Function to return the required count
int cntSplits(string s)
{
    // If the splitting is not possible
    if (s[s.size() - 1] == '1')
        return 0;
  
    // To store the count of zeroes
    int c_zero = 0;
  
    // Counting the number of zeroes
    for (int i = 0; i < s.size(); i++)
        c_zero += (s[i] == '0');
  
    // Return the final answer
    return (int)pow(2, c_zero - 1);
}
  
// Driver code
int main()
{
    string s = "10010";
  
    cout << cntSplits(s);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
  
static int maxN = 20;
static int maxM = 64;
  
// Function to return the required count
static int cntSplits(String s)
{
    // If the splitting is not possible
    if (s.charAt(s.length() - 1) == '1')
        return 0;
  
    // To store the count of zeroes
    int c_zero = 0;
  
    // Counting the number of zeroes
    for (int i = 0; i < s.length(); i++)
        c_zero += (s.charAt(i) == '0') ? 1 : 0;
  
    // Return the final answer
    return (int)Math.pow(2, c_zero - 1);
}
  
// Driver code
public static void main(String []args)
{
    String s = "10010";
  
    System.out.println(cntSplits(s));
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach 
  
# Function to return the required count 
def cntSplits(s) :
  
    # If the splitting is not possible 
    if (s[len(s) - 1] == '1') :
        return 0; 
  
    # To store the count of zeroes 
    c_zero = 0; 
  
    # Counting the number of zeroes 
    for i in range(len(s)) :
        c_zero += (s[i] == '0'); 
  
    # Return the final answer 
    return int(pow(2, c_zero - 1)); 
  
# Driver code 
if __name__ == "__main__" : 
  
    s = "10010"; 
  
    print(cntSplits(s));
      
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
                      
class GFG 
{
  
static int maxN = 20;
static int maxM = 64;
  
// Function to return the required count
static int cntSplits(String s)
{
    // If the splitting is not possible
    if (s[s.Length - 1] == '1')
        return 0;
  
    // To store the count of zeroes
    int c_zero = 0;
  
    // Counting the number of zeroes
    for (int i = 0; i < s.Length; i++)
        c_zero += (s[i] == '0') ? 1 : 0;
  
    // Return the final answer
    return (int)Math.Pow(2, c_zero - 1);
}
  
// Driver code
public static void Main(String []args)
{
    String s = "10010";
  
    Console.WriteLine(cntSplits(s));
}
}
  
// This code is contributed by 29AjayKumar


输出:
4