📌  相关文章
📜  重复K次的二进制字符串中0的最长子字符串

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

重复K次的二进制字符串中0的最长子字符串

给定大小为N的二进制字符串S和一个数字K 。任务是在通过重复给定字符串K 次形成的字符串中找到最长的 0 子字符串。
例子:


方法:

  1. 如果K 是 one ,则使用简单循环在字符串中找到最长的 0 子字符串
  2. 如果K 大于一,则将给定字符串添加到字符串的末尾。然后字符串变为 S+S 并且长度将是 2*N 并使用简单的循环在字符串中找到最长的 0 子串
    • 如果最长的子串是 2*N,那么我们的答案就是 K*N
    • 否则这将是我们需要的答案

下面是上述方法的实现:

C++
// C++ program to find the find the Longest continuous
// sequence of '0' after repeating Given string K time
#include 
using namespace std;
 
// Function to find the longest substring of 0's
int longest_substring(string s, int k)
{
    // To store size of the string
    int n = s.size();
     
    if(k>1)
    {
        s += s;
        n *= 2;
    }   
     
    // To store the required answer
    int ans = 0;
     
    // Find the longest substring of 0's
    int i = 0;
    while (i < n)
    {
        int x = 0;
         
        // Run a loop upto s[i] is zero
        while (s[i] == '0' && i < n)
            x++, i++;
        ans = max(ans, x);
        i++;
    }
     
    // Check the conditions
    if(k==1 or ans!=n)
        return ans;
         
    else
        return (ans/2)*k;
}
 
// Driver code
int main()
{
    string s = "010001000";
     
    int k = 4;
     
    // Function call
    cout << longest_substring(s, k);
     
    return 0;
}


Java
// Java program to find the Longest continuous
// sequence of '0' after repeating Given string K time
class GFG
{
 
// Function to find the longest substring of 0's
static int longest_substring(String s, int k)
{
    // To store size of the string
    int n = s.length();
     
    if(k > 1)
    {
        s += s;
        n *= 2;
    }
     
    // To store the required answer
    int ans = 0;
     
    // Find the longest substring of 0's
    int i = 0;
    while (i < n)
    {
        int x = 0;
         
        // Run a loop upto s[i] is zero
        while (i < n && s.charAt(i) == '0')
        {
            x++; i++;
        }
        ans = Math.max(ans, x);
        i++;
    }
     
    // Check the conditions
    if(k == 1 || ans != n)
        return ans;
         
    else
        return (ans / 2) * k;
}
 
// Driver code
public static void main(String[] args)
{
    String s = "010001000";
     
    int k = 4;
     
    // Function call
    System.out.println(longest_substring(s, k));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to find the find the Longest continuous
# sequence of '0' after repeating Given K time
 
# Function to find the longest subof 0's
def longest_substring(s, k):
    # To store size of the string
    n = len(s)
 
    if(k>1):
        s += s
        n *= 2
 
    # To store the required answer
    ans = 0
 
    # Find the longest subof 0's
    i = 0
    while (i < n):
        x = 0
 
        # Run a loop upto s[i] is zero
        while (i < n and s[i] == '0'):
            x,i=x+1, i+1
        ans = max(ans, x)
        i+=1
 
    # Check the conditions
    if(k==1 or ans!=n):
        return ans
 
    else:
        return (ans//2)*k
 
 
# Driver code
 
s = "010001000"
 
k = 4
 
# Function call
print(longest_substring(s, k))
 
# This code is contributed by mohit kumar 29


C#
// C# program to find the Longest continuous
// sequence of '0' after repeating Given string K time
using System;
     
class GFG
{
 
// Function to find the longest substring of 0's
static int longest_substring(String s, int k)
{
    // To store size of the string
    int n = s.Length;
     
    if(k > 1)
    {
        s += s;
        n *= 2;
    }
     
    // To store the required answer
    int ans = 0;
     
    // Find the longest substring of 0's
    int i = 0;
    while (i < n)
    {
        int x = 0;
         
        // Run a loop upto s[i] is zero
        while (i < n && s[i] == '0')
        {
            x++; i++;
        }
        ans = Math.Max(ans, x);
        i++;
    }
     
    // Check the conditions
    if(k == 1 || ans != n)
        return ans;
         
    else
        return (ans / 2) * k;
}
 
// Driver code
public static void Main(String[] args)
{
    String s = "010001000";
     
    int k = 4;
     
    // Function call
    Console.WriteLine(longest_substring(s, k));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:

4