📌  相关文章
📜  二进制字符串的最大拆分数,以使每个子字符串可被给定的奇数整除

📅  最后修改于: 2021-06-27 01:08:59             🧑  作者: Mango

给定二进制字符串str ,任务是计算最大可能的拆分,以使每个子字符串可被给定的奇数K整除。
例子:

方法:为了解决此问题,我们从字符串的末尾开始遍历并生成所遍历长度的总和。一旦总和整除K,我们通过1和复位增加数量为0,穿越着,重复同样的过程。完整遍历字符串,如果将sum重置为0,则count的值将给出所需的最大可能分割数。否则,请打印“不可能”,因为所有段都不能被K整除。
下面的代码是上述方法的实现:

C++
// C++ Program to split
// a given binary string
// into maximum possible
// segments divisible by
// given odd number K
 
#include 
using namespace std;
// Function to calculate
// maximum splits possible
void max_segments(string str, int K)
{
    int n = str.length();
    int s = 0, sum = 0, count = 0;
    for (int i = n - 1; i >= 0; i--) {
        int a = str[i] - '0';
        sum += a * pow(2, s);
 
        s++;
        if (sum != 0 && sum % K == 0) {
            count++;
            sum = 0;
            s = 0;
        }
    }
    if (sum != 0)
        cout << "-1" << endl;
    else
        cout << count << endl;
}
 
// Driver code
int main()
{
    string str = "10111001";
    int K = 5;
    max_segments(str, K);
    return 0;
}


Java
// Java code to split a given
// binary string into maximum
// possible segments divisible
// by given odd number K
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to calculate
// maximum splits possible
static void rearrange(String str, int K)
{
    int n = str.length();
    int s = 0, sum = 0, count = 0;
     
    for(int i = n - 1; i >= 0; i--)
    {
       int a = str.charAt(i) - '0';
       sum += a * Math.pow(2, s);
       s++;
        
       if (sum != 0 && sum % K == 0)
       {
           count++;
           sum = 0;
             s = 0;
       }
    }
  
    if (sum != 0)
        System.out.println("-1");   
    else
        System.out.println(count);
}
 
// Driver code
public static void main(String[] args)
{
    String str = "10111001";
    int K = 5;
     
    rearrange(str, K);
}
}
 
// This code is contributed by coder001


Python3
# Python3 program to split
# a given binary string
# into maximum possible
# segments divisible by
# given odd number K
 
# Function to calculate
# maximum splits possible
def max_segments(st, K):
 
    n = len(st)
    s, sum, count = 0, 0, 0
     
    for i in range(n - 1, -1, -1):
        a = ord(st[i]) - 48
        sum += a * pow(2, s)
        s += 1
         
        if (sum != 0 and sum % K == 0):
            count += 1
            sum = 0
            s = 0
             
    if (sum != 0):
        print("-1")
    else:
        print(count)
 
# Driver code
if __name__ == "__main__":
     
    st = "10111001"
    K = 5
    max_segments(st, K)
 
# This code is contributed by chitranayal


C#
// C# program to split a given
// binary string into maximum
// possible segments divisible by
// given odd number K
using System;
 
class GFG{
     
// Function to calculate
// maximum splits possible
static void max_segments(string str, int K)
{
    int n = str.Length;
    int s = 0;
    int sum = 0;
    int count = 0;
     
    for(int i = n - 1; i >= 0; i--)
    {
       int a = str[i] - '0';
       sum += a * (int)Math.Pow(2, s);
       s++;
        
       if (sum != 0 && sum % K == 0)
       {
           count++;
           sum = 0;
             s = 0;
       }
    }
    if (sum != 0)
    {
        Console.Write("-1");
    }
    else
    {
        Console.Write(count);
    }
}
 
// Driver code
public static void Main()
{
    string str = "10111001";
    int K = 5;
     
    max_segments(str, K);
}
}
 
// This code is contributed by sayesha


Javascript


输出:
2

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。