📌  相关文章
📜  在二进制字符串的任何循环中连续放置在开始和结束处的最大 0 数

📅  最后修改于: 2021-10-25 09:21:18             🧑  作者: Mango

给定一个大小为N的二进制字符串S ,任务是最大化出现在给定字符串S的任何旋转的开始和结束处的连续0的计数总和。

例子:

朴素的方法:最简单的想法是生成给定字符串的所有旋转,对于每个旋转,计算字符串开头和结尾处出现的 0 的数量并计算它们的总和。最后,打印获得的最大和。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum sum of
// consecutive 0s present at the start
// and end of a string present in any
// of the rotations of the given string
void findMaximumZeros(string str, int n)
{
    // Check if all the characters
    // in the string are 0
    int c0 = 0;
 
    // Iterate over characters
    // of the string
    for (int i = 0; i < n; ++i) {
        if (str[i] == '0')
            c0++;
    }
 
    // If the frequency of '1' is 0
    if (c0 == n) {
 
        // Print n as the result
        cout << n;
        return;
    }
 
    // Concatenate the string
    // with itself
    string s = str + str;
 
    // Stores the required result
    int mx = 0;
 
    // Generate all rotations of the string
    for (int i = 0; i < n; ++i) {
 
        // Store the number of consecutive 0s
        // at the start and end of the string
        int cs = 0;
        int ce = 0;
 
        // Count 0s present at the start
        for (int j = i; j < i + n; ++j) {
            if (s[j] == '0')
                cs++;
            else
                break;
        }
 
        // Count 0s present at the end
        for (int j = i + n - 1; j >= i; --j) {
            if (s[j] == '0')
                ce++;
            else
                break;
        }
 
        // Calculate the sum
        int val = cs + ce;
 
        // Update the overall
        // maximum sum
        mx = max(val, mx);
    }
 
    // Print the result
    cout << mx;
}
 
// Driver Code
int main()
{
    // Given string
    string s = "1001";
 
    // Store the size of the string
    int n = s.size();
 
    findMaximumZeros(s, n);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the maximum sum of
// consecutive 0s present at the start
// and end of a string present in any
// of the rotations of the given string
static void findMaximumZeros(String str, int n)
{
     
    // Check if all the characters
    // in the string are 0
    int c0 = 0;
 
    // Iterate over characters
    // of the string
    for(int i = 0; i < n; ++i)
    {
        if (str.charAt(i) == '0')
            c0++;
    }
 
    // If the frequency of '1' is 0
    if (c0 == n)
    {
         
        // Print n as the result
        System.out.print(n);
        return;
    }
 
    // Concatenate the string
    // with itself
    String s = str + str;
 
    // Stores the required result
    int mx = 0;
 
    // Generate all rotations of the string
    for(int i = 0; i < n; ++i)
    {
         
        // Store the number of consecutive 0s
        // at the start and end of the string
        int cs = 0;
        int ce = 0;
 
        // Count 0s present at the start
        for(int j = i; j < i + n; ++j)
        {
            if (s.charAt(j) == '0')
                cs++;
            else
                break;
        }
 
        // Count 0s present at the end
        for(int j = i + n - 1; j >= i; --j)
        {
            if (s.charAt(j) == '0')
                ce++;
            else
                break;
        }
 
        // Calculate the sum
        int val = cs + ce;
 
        // Update the overall
        // maximum sum
        mx = Math.max(val, mx);
    }
 
    // Print the result
    System.out.print(mx);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given string
    String s = "1001";
 
    // Store the size of the string
    int n = s.length();
 
    findMaximumZeros(s, n);
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program for the above approach
 
# Function to find the maximum sum of
# consecutive 0s present at the start
# and end of a string present in any
# of the rotations of the given string
def findMaximumZeros(st, n):
 
    # Check if all the characters
    # in the string are 0
    c0 = 0
 
    # Iterate over characters
    # of the string
    for i in range(n):
        if (st[i] == '0'):
            c0 += 1
 
    # If the frequency of '1' is 0
    if (c0 == n):
 
        # Print n as the result
        print(n)
        return
 
    # Concatenate the string
    # with itself
    s = st + st
 
    # Stores the required result
    mx = 0
 
    # Generate all rotations of the string
    for i in range(n):
 
        # Store the number of consecutive 0s
        # at the start and end of the string
        cs = 0
        ce = 0
 
        # Count 0s present at the start
        for j in range(i, i + n):
            if (s[j] == '0'):
                cs += 1
            else:
                break
 
        # Count 0s present at the end
        for j in range(i + n - 1, i - 1, -1):
            if (s[j] == '0'):
                ce += 1
            else:
                break
 
        # Calculate the sum
        val = cs + ce
 
        # Update the overall
        # maximum sum
        mx = max(val, mx)
 
    # Print the result
    print(mx)
 
# Driver Code
if __name__ == "__main__":
 
    # Given string
    s = "1001"
 
    # Store the size of the string
    n = len(s)
 
    findMaximumZeros(s, n)
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the maximum sum of
// consecutive 0s present at the start
// and end of a string present in any
// of the rotations of the given string
static void findMaximumZeros(string str, int n)
{
     
    // Check if all the characters
    // in the string are 0
    int c0 = 0;
 
    // Iterate over characters
    // of the string
    for(int i = 0; i < n; ++i)
    {
        if (str[i] == '0')
            c0++;
    }
 
    // If the frequency of '1' is 0
    if (c0 == n)
    {
         
        // Print n as the result
        Console.Write(n);
        return;
    }
 
    // Concatenate the string
    // with itself
    string s = str + str;
 
    // Stores the required result
    int mx = 0;
 
    // Generate all rotations of the string
    for(int i = 0; i < n; ++i)
    {
         
        // Store the number of consecutive 0s
        // at the start and end of the string
        int cs = 0;
        int ce = 0;
 
        // Count 0s present at the start
        for(int j = i; j < i + n; ++j)
        {
            if (s[j] == '0')
                cs++;
            else
                break;
        }
 
        // Count 0s present at the end
        for(int j = i + n - 1; j >= i; --j)
        {
            if (s[j] == '0')
                ce++;
            else
                break;
        }
 
        // Calculate the sum
        int val = cs + ce;
 
        // Update the overall
        // maximum sum
        mx = Math.Max(val, mx);
    }
 
    // Print the result
    Console.Write(mx);
}
 
// Driver Code
public static void Main(string[] args)
{
     
    // Given string
    string s = "1001";
 
    // Store the size of the string
    int n = s.Length;
 
    findMaximumZeros(s, n);
}
}
 
// This code is contributed by AnkThon


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum sum of
// consecutive 0s present at the start
// and end of any rotation of the string str
void findMaximumZeros(string str, int n)
{
    // Stores the count of 0s
    int c0 = 0;
    for (int i = 0; i < n; ++i) {
        if (str[i] == '0')
            c0++;
    }
 
    // If the frequency of '1' is 0
    if (c0 == n) {
 
        // Print n as the result
        cout << n;
        return;
    }
 
    // Stores the required sum
    int mx = 0;
 
    // Find the maximum consecutive
    // length of 0s present in the string
    int cnt = 0;
 
    for (int i = 0; i < n; i++) {
        if (str[i] == '0')
            cnt++;
        else {
            mx = max(mx, cnt);
            cnt = 0;
        }
    }
 
    // Update the overall maximum sum
    mx = max(mx, cnt);
 
    // Find the number of 0s present at
    // the start and end of the string
    int start = 0, end = n - 1;
    cnt = 0;
 
    // Update the count of 0s at the start
    while (str[start] != '1' && start < n) {
        cnt++;
        start++;
    }
 
    // Update the count of 0s at the end
    while (str[end] != '1' && end >= 0) {
        cnt++;
        end--;
    }
 
    // Update the maximum sum
    mx = max(mx, cnt);
 
    // Print the result
    cout << mx;
}
 
// Driver Code
int main()
{
    // Given string
    string s = "1001";
 
    // Store the size of the string
    int n = s.size();
 
    findMaximumZeros(s, n);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the maximum sum of
// consecutive 0s present at the start
// and end of any rotation of the string str
static void findMaximumZeros(String str, int n)
{
     
    // Stores the count of 0s
    int c0 = 0;
    for(int i = 0; i < n; ++i)
    {
        if (str.charAt(i) == '0')
            c0++;
    }
 
    // If the frequency of '1' is 0
    if (c0 == n)
    {
         
        // Print n as the result
        System.out.print(n);
        return;
    }
 
    // Stores the required sum
    int mx = 0;
 
    // Find the maximum consecutive
    // length of 0s present in the string
    int cnt = 0;
 
    for(int i = 0; i < n; i++)
    {
        if (str.charAt(i) == '0')
            cnt++;
        else
        {
            mx = Math.max(mx, cnt);
            cnt = 0;
        }
    }
 
    // Update the overall maximum sum
    mx = Math.max(mx, cnt);
 
    // Find the number of 0s present at
    // the start and end of the string
    int start = 0, end = n - 1;
    cnt = 0;
 
    // Update the count of 0s at the start
    while (str.charAt(start) != '1' && start < n)
    {
        cnt++;
        start++;
    }
 
    // Update the count of 0s at the end
    while (str.charAt(end) != '1' && end >= 0)
    {
        cnt++;
        end--;
    }
 
    // Update the maximum sum
    mx = Math.max(mx, cnt);
 
    // Print the result
    System.out.println(mx);
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given string
    String s = "1001";
 
    // Store the size of the string
    int n = s.length();
 
    findMaximumZeros(s, n);
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to find the maximum sum of
# consecutive 0s present at the start
# and end of any rotation of the string str
def findMaximumZeros(string, n):
     
    # Stores the count of 0s
    c0 = 0
     
    for i in range(n):
        if (string[i] == '0'):
            c0 += 1
 
    # If the frequency of '1' is 0
    if (c0 == n):
 
        # Print n as the result
        print(n, end = "")
        return
 
    # Stores the required sum
    mx = 0
 
    # Find the maximum consecutive
    # length of 0s present in the string
    cnt = 0
 
    for i in range(n):
        if (string[i] == '0'):
            cnt += 1
        else:
            mx = max(mx, cnt)
            cnt = 0
 
    # Update the overall maximum sum
    mx = max(mx, cnt)
 
    # Find the number of 0s present at
    # the start and end of the string
    start = 0
    end = n - 1
    cnt = 0
 
    # Update the count of 0s at the start
    while (string[start] != '1' and start < n):
        cnt += 1
        start += 1
 
    # Update the count of 0s at the end
    while (string[end] != '1' and  end >= 0):
        cnt += 1
        end -= 1
 
    # Update the maximum sum
    mx = max(mx, cnt)
 
    # Print the result
    print(mx, end = "")
 
# Driver Code
if __name__ == "__main__":
 
    # Given string
    s = "1001"
 
    # Store the size of the string
    n = len(s)
 
    findMaximumZeros(s, n)
 
# This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the maximum sum of
// consecutive 0s present at the start
// and end of any rotation of the string str
static void findMaximumZeros(string str, int n)
{
     
    // Stores the count of 0s
    int c0 = 0;
    for(int i = 0; i < n; ++i)
    {
        if (str[i] == '0')
            c0++;
    }
 
    // If the frequency of '1' is 0
    if (c0 == n)
    {
         
        // Print n as the result
         Console.Write(n);
        return;
    }
 
    // Stores the required sum
    int mx = 0;
 
    // Find the maximum consecutive
    // length of 0s present in the string
    int cnt = 0;
 
    for(int i = 0; i < n; i++)
    {
        if (str[i] == '0')
            cnt++;
        else
        {
            mx = Math.Max(mx, cnt);
            cnt = 0;
        }
    }
 
    // Update the overall maximum sum
    mx = Math.Max(mx, cnt);
 
    // Find the number of 0s present at
    // the start and end of the string
    int start = 0, end = n - 1;
    cnt = 0;
 
    // Update the count of 0s at the start
    while (str[start] != '1' && start < n)
    {
        cnt++;
        start++;
    }
 
    // Update the count of 0s at the end
    while (str[end] != '1' && end >= 0)
    {
        cnt++;
        end--;
    }
 
    // Update the maximum sum
    mx = Math.Max(mx, cnt);
 
    // Print the result
    Console.Write(mx);
}
 
// Driver Code
static public void Main ()
{
     
    // Given string
    string s = "1001";
 
    // Store the size of the string
    int n = s.Length;
 
    findMaximumZeros(s, n);
}
}
 
// This code is contributed by avijitmondal1998


Javascript


输出:
2

时间复杂度: O(N 2 )
辅助空间: O(N)

有效的方法:这个想法是在给定的字符串找到最大数量的连续 0。另外,找到字符串开头和结尾的连续0的总和,然后打印出它们中的最大值。
请按照以下步骤解决问题:

  • 检查字符串中’1’的频率, S是否等于0 。如果发现为真,则打印N的值作为结果。
  • 否则,请执行以下步骤:
    • 将给定字符串中连续 0 的最大数量存储在一个变量中,比如X
    • 初始化两个变量,0开始,到N-1结束
    • S[start]不等于“ 1 ”时,增加cnt的值并1开始
    • 递增1 CNT和递减的值,而S [结束]不等于“1”。
    • 结果打印Xcnt的最大值。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum sum of
// consecutive 0s present at the start
// and end of any rotation of the string str
void findMaximumZeros(string str, int n)
{
    // Stores the count of 0s
    int c0 = 0;
    for (int i = 0; i < n; ++i) {
        if (str[i] == '0')
            c0++;
    }
 
    // If the frequency of '1' is 0
    if (c0 == n) {
 
        // Print n as the result
        cout << n;
        return;
    }
 
    // Stores the required sum
    int mx = 0;
 
    // Find the maximum consecutive
    // length of 0s present in the string
    int cnt = 0;
 
    for (int i = 0; i < n; i++) {
        if (str[i] == '0')
            cnt++;
        else {
            mx = max(mx, cnt);
            cnt = 0;
        }
    }
 
    // Update the overall maximum sum
    mx = max(mx, cnt);
 
    // Find the number of 0s present at
    // the start and end of the string
    int start = 0, end = n - 1;
    cnt = 0;
 
    // Update the count of 0s at the start
    while (str[start] != '1' && start < n) {
        cnt++;
        start++;
    }
 
    // Update the count of 0s at the end
    while (str[end] != '1' && end >= 0) {
        cnt++;
        end--;
    }
 
    // Update the maximum sum
    mx = max(mx, cnt);
 
    // Print the result
    cout << mx;
}
 
// Driver Code
int main()
{
    // Given string
    string s = "1001";
 
    // Store the size of the string
    int n = s.size();
 
    findMaximumZeros(s, n);
 
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the maximum sum of
// consecutive 0s present at the start
// and end of any rotation of the string str
static void findMaximumZeros(String str, int n)
{
     
    // Stores the count of 0s
    int c0 = 0;
    for(int i = 0; i < n; ++i)
    {
        if (str.charAt(i) == '0')
            c0++;
    }
 
    // If the frequency of '1' is 0
    if (c0 == n)
    {
         
        // Print n as the result
        System.out.print(n);
        return;
    }
 
    // Stores the required sum
    int mx = 0;
 
    // Find the maximum consecutive
    // length of 0s present in the string
    int cnt = 0;
 
    for(int i = 0; i < n; i++)
    {
        if (str.charAt(i) == '0')
            cnt++;
        else
        {
            mx = Math.max(mx, cnt);
            cnt = 0;
        }
    }
 
    // Update the overall maximum sum
    mx = Math.max(mx, cnt);
 
    // Find the number of 0s present at
    // the start and end of the string
    int start = 0, end = n - 1;
    cnt = 0;
 
    // Update the count of 0s at the start
    while (str.charAt(start) != '1' && start < n)
    {
        cnt++;
        start++;
    }
 
    // Update the count of 0s at the end
    while (str.charAt(end) != '1' && end >= 0)
    {
        cnt++;
        end--;
    }
 
    // Update the maximum sum
    mx = Math.max(mx, cnt);
 
    // Print the result
    System.out.println(mx);
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given string
    String s = "1001";
 
    // Store the size of the string
    int n = s.length();
 
    findMaximumZeros(s, n);
}
}
 
// This code is contributed by sanjoy_62

蟒蛇3

# Python3 program for the above approach
 
# Function to find the maximum sum of
# consecutive 0s present at the start
# and end of any rotation of the string str
def findMaximumZeros(string, n):
     
    # Stores the count of 0s
    c0 = 0
     
    for i in range(n):
        if (string[i] == '0'):
            c0 += 1
 
    # If the frequency of '1' is 0
    if (c0 == n):
 
        # Print n as the result
        print(n, end = "")
        return
 
    # Stores the required sum
    mx = 0
 
    # Find the maximum consecutive
    # length of 0s present in the string
    cnt = 0
 
    for i in range(n):
        if (string[i] == '0'):
            cnt += 1
        else:
            mx = max(mx, cnt)
            cnt = 0
 
    # Update the overall maximum sum
    mx = max(mx, cnt)
 
    # Find the number of 0s present at
    # the start and end of the string
    start = 0
    end = n - 1
    cnt = 0
 
    # Update the count of 0s at the start
    while (string[start] != '1' and start < n):
        cnt += 1
        start += 1
 
    # Update the count of 0s at the end
    while (string[end] != '1' and  end >= 0):
        cnt += 1
        end -= 1
 
    # Update the maximum sum
    mx = max(mx, cnt)
 
    # Print the result
    print(mx, end = "")
 
# Driver Code
if __name__ == "__main__":
 
    # Given string
    s = "1001"
 
    # Store the size of the string
    n = len(s)
 
    findMaximumZeros(s, n)
 
# This code is contributed by AnkThon

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the maximum sum of
// consecutive 0s present at the start
// and end of any rotation of the string str
static void findMaximumZeros(string str, int n)
{
     
    // Stores the count of 0s
    int c0 = 0;
    for(int i = 0; i < n; ++i)
    {
        if (str[i] == '0')
            c0++;
    }
 
    // If the frequency of '1' is 0
    if (c0 == n)
    {
         
        // Print n as the result
         Console.Write(n);
        return;
    }
 
    // Stores the required sum
    int mx = 0;
 
    // Find the maximum consecutive
    // length of 0s present in the string
    int cnt = 0;
 
    for(int i = 0; i < n; i++)
    {
        if (str[i] == '0')
            cnt++;
        else
        {
            mx = Math.Max(mx, cnt);
            cnt = 0;
        }
    }
 
    // Update the overall maximum sum
    mx = Math.Max(mx, cnt);
 
    // Find the number of 0s present at
    // the start and end of the string
    int start = 0, end = n - 1;
    cnt = 0;
 
    // Update the count of 0s at the start
    while (str[start] != '1' && start < n)
    {
        cnt++;
        start++;
    }
 
    // Update the count of 0s at the end
    while (str[end] != '1' && end >= 0)
    {
        cnt++;
        end--;
    }
 
    // Update the maximum sum
    mx = Math.Max(mx, cnt);
 
    // Print the result
    Console.Write(mx);
}
 
// Driver Code
static public void Main ()
{
     
    // Given string
    string s = "1001";
 
    // Store the size of the string
    int n = s.Length;
 
    findMaximumZeros(s, n);
}
}
 
// This code is contributed by avijitmondal1998

Javascript


输出:
2

时间复杂度: O(N)
辅助空间: O(1)