📌  相关文章
📜  连接二进制字符串中的最大连续零

📅  最后修改于: 2022-05-13 01:57:06.406000             🧑  作者: Mango

连接二进制字符串中的最大连续零

你得到一个长度为n的二进制字符串str 。假设您通过将 str 的k个副本连接在一起来创建另一个大小为 n * k 的字符串。仅由 0 组成的连接字符串的子字符串的最大大小是多少?鉴于 k > 1。

例子:

如果给定字符串全为零,则答案为 n * k。如果 S 包含 1,则答案要么是 str 的仅包含零的子串的最大长度,要么是 S 的仅包含零的最大前缀的长度与 str 的仅包含零的最大后缀的长度之和。只有当 k > 1 时才必须计算最后一个。

C++
// C++ program to find maximum number 
// of consecutive zeroes after 
// concatenating a binary string
#include
using namespace std;
  
// returns the maximum size of a 
// substring consisting only of 
// zeroes after k concatenation
int max_length_substring(string st, 
                         int n, int k)
{
  
    // stores the maximum length 
    // of the required substring
    int max_len = 0;
  
    int len = 0;
    for (int i = 0; i < n; ++i) 
    {
  
        // if the current character is 0
        if (st[i] == '0')
            len++;
        else
            len = 0;
  
        // stores maximum length of current
        // substrings with zeroes
        max_len = max(max_len, len);
    }
  
    // if the whole string is
    // filled with zero
    if (max_len == n)
        return n * k;
  
    int pref = 0, suff = 0;
  
    // computes the length of the maximal
    // prefix which contains only zeroes
    for (int i = 0; st[i] == '0';
                    ++i, ++pref);
  
    // computes the length of the maximal 
    // suffix which contains only zeroes
    for (int i = n - 1; st[i] == '0'; 
                        --i, ++suff);
  
    // if more than 1 concatenations
    // are to be made
    if (k > 1)
        max_len = max(max_len, 
                 pref + suff);
  
    return max_len;
}
  
// Driver code
int main()
{
    int n = 6;
    int k = 3;
    string st = "110010";
    int ans = max_length_substring(st, n, k);
  
    cout << ans;
}
  
// This code is contributed by ihritik


Java
// Java program to find maximum number of
// consecutive zeroes after concatenating
// a binary string
  
class GFG {
  
    // returns the maximum size of a substring
    // consisting only of zeroes
    // after k concatenation
    static int max_length_substring(String st,
                                    int n, int k)
    {
  
        // stores the maximum length of the
        // required substring
        int max_len = 0;
  
        int len = 0;
        for (int i = 0; i < n; ++i) {
  
            // if the current character is 0
            if (st.charAt(i) == '0')
                len++;
            else
                len = 0;
  
            // stores maximum length of current
            // substrings with zeroes
            max_len = Math.max(max_len, len);
        }
  
        // if the whole string is filled with zero
        if (max_len == n)
            return n * k;
  
        int pref = 0, suff = 0;
  
        // computes the length of the maximal
        // prefix which contains only zeroes
        for (int i = 0; st.charAt(i) == '0'; ++i, ++pref)
            ;
  
        // computes the length of the maximal 
        // suffix which contains only zeroes
        for (int i = n - 1; st.charAt(i) == '0'; --i, ++suff)
            ;
  
        // if more than 1 concatenations are to be made
        if (k > 1)
            max_len = Math.max(max_len, pref + suff);
  
        return max_len;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 6;
        int k = 3;
        String st = "110010";
        int ans = max_length_substring(st, n, k);
  
        System.out.println(ans);
    }
}


Python3
# Python3 program to find maximum 
# number of consecutive zeroes 
# after concatenating a binary string
  
# returns the maximum size of a 
# substring consisting only of 
# zeroes after k concatenation
def max_length_substring(st, n, k):
  
    # stores the maximum length 
    # of the required substring
    max_len = 0
  
    len = 0
    for i in range(0, n):
  
        # if the current character is 0
        if (st[i] == '0'):
            len = len + 1;
        else:
            len = 0
  
        # stores maximum length of 
        # current substrings with zeroes
        max_len = max(max_len, len)
      
  
    # if the whole is filled 
    # with zero
    if (max_len == n):
        return n * k
  
    pref = 0
    suff = 0
  
    # computes the length of the maximal
    # prefix which contains only zeroes
    i = 0
    while(st[i] == '0'):
        i = i + 1
        pref = pref + 1
  
    # computes the length of the maximal 
    # suffix which contains only zeroes
    i = n - 1
    while(st[i] == '0'):
        i = i - 1
        suff = suff + 1
  
    # if more than 1 concatenations 
    # are to be made
    if (k > 1):
        max_len = max(max_len, 
                      pref + suff)
  
    return max_len
  
# Driver code
n = 6
k = 3
st = "110010"
ans = max_length_substring(st, n, k)
  
print(ans)
  
# This code is contributed by ihritik


C#
// C# program to find maximum number 
// of consecutive zeroes after 
// concatenating a binary string
using System;
  
class GFG 
{
  
// returns the maximum size of 
// a substring consisting only 
// of zeroes after k concatenation
static int max_length_substring(string st,
                                int n, int k)
{
  
    // stores the maximum length 
    // of the required substring
    int max_len = 0;
  
    int len = 0;
    for (int i = 0; i < n; ++i) 
    {
  
        // if the current character is 0
        if (st[i] == '0')
            len++;
        else
            len = 0;
  
        // stores maximum length of current
        // substrings with zeroes
        max_len = Math.Max(max_len, len);
    }
  
    // if the whole string is 
    // filled with zero
    if (max_len == n)
        return n * k;
  
    int pref = 0, suff = 0;
  
    // computes the length of the maximal
    // prefix which contains only zeroes
    for (int i = 0; st[i] == '0'; 
                    ++i, ++pref);
  
    // computes the length of the maximal 
    // suffix which contains only zeroes
    for (int i = n - 1; st[i] == '0';
                        --i, ++suff);
  
    // if more than 1 concatenations 
    // are to be made
    if (k > 1)
        max_len = Math.Max(max_len, 
                           pref + suff);
  
    return max_len;
}
  
// Driver code
public static void Main(string[] args)
{
    int n = 6;
    int k = 3;
    string st = "110010";
    int ans = max_length_substring(st, n, k);
  
    Console.WriteLine(ans);
}
}
  
// This code is contributed by ihritik


PHP
 1)
        $max_len = max($max_len, 
                       $pref + $suff);
  
    return $max_len;
}
  
// Driver code
$n = 6;
$k = 3;
$st = "110010";
$ans = max_length_substring($st, $n, $k);
  
echo $ans;
  
// This code is contributed by ihritik
?>


输出:
2