📌  相关文章
📜  最小化二进制字符串中的删除以删除形式为“0101”的所有子序列

📅  最后修改于: 2021-09-03 02:57:18             🧑  作者: Mango

给定一个长度为N的二进制字符串S ,任务是找到需要从字符串删除的最少字符数,使得字符串中不存在“0101”形式的子字符串。

例子:

处理方法:按照以下步骤解决问题:

  • 所需的有效字符串最多可以包含三个相同元素的块,即字符串可以是以下模式之一“00…0”、“11…1”、“00…01…1”、“1…10”。 .0”、“00..01…10..0”、“1…10…01…1”
  • 使用部分和计算块的0 s 和1 s 的频率。
  • 固定0 s 和1 s 块的开始和结束索引,并确定计算出的部分和需要删除的最小字符数。
  • 因此,检查通过删除给定类型的子序列可以获得的最长字符串的长度。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find minimum characters
// to be removed such that no subsequence
// of the form "0101" exists in the string
int findmin(string s)
{
    int n = s.length();
    int i, j, maximum = 0;
 
    // Stores the partial sums
    int incr[n + 1] = { 0 };
 
    for (i = 0; i < n; i++) {
 
        // Calculate partial sums
        incr[i + 1] = incr[i];
 
        if (s[i] == '0') {
            incr[i + 1]++;
        }
    }
 
    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
 
            // Setting endpoints and
            // deleting characters indices.
            maximum
                = max(maximum, incr[i] + j - i + 1
                                   - (incr[j + 1] - incr[i])
                                   + incr[n] - incr[j + 1]);
        }
    }
 
    // Return count of deleted characters
    return n - maximum;
}
// Driver Code
int main()
{
    string S = "0110100110";
    int minimum = findmin(S);
    cout << minimum << '\n';
}


Java
// Java Program to implement
// the above approach
import java.io.*;
class GFG{
 
// Function to find minimum
// characters to be removed
// such that no subsequence
// of the form "0101" exists
// in the string
static int findmin(String s)
{
  int n = s.length();
  int i, j, maximum = 0;
 
  // Stores the partial sums
  int[] incr = new int[n + 1];
 
  for (i = 0; i < n; i++)
  {
    // Calculate partial sums
    incr[i + 1] = incr[i];
 
    if (s.charAt(i) == '0')
    {
      incr[i + 1]++;
    }
  }
 
  for (i = 0; i < n; i++)
  {
    for (j = i + 1; j < n; j++)
    {
      // Setting endpoints and
      // deleting characters indices.
      maximum = Math.max(maximum, incr[i] +
                         j - i + 1 -
                        (incr[j + 1] - incr[i]) +
                         incr[n] - incr[j + 1]);
    }
  }
 
  // Return count of
  // deleted characters
  return n - maximum;
}
   
// Driver Code
public static void main(String[] args)
{
  String S = "0110100110";
  int minimum = findmin(S);
  System.out.println(minimum);
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 Program to implement
# the above approach
 
# Function to find minimum
# characters to be removed
# such that no subsequence
# of the form "0101" exists
# in the string
def findmin(s):
 
    n = len(s)
    maximum = 0
 
    # Stores the partial sums
    incr = [0] * (n + 1)
     
    for i in range(0, n):
       
        # Calculate partial sums
        incr[i + 1] = incr[i]
 
        if (s[i] == '0'):
            incr[i + 1] = incr[i + 1] + 1
 
    for i in range(0, n + 1):
        for j in range(i + 1, n):
 
            # Setting endpoints and
            # deleting characters indices.
            maximum = max(maximum, incr[i] +
                          j - i + 1 -
                         (incr[j + 1] - incr[i]) +
                          incr[n] - incr[j + 1])
 
    # Return count of
    # deleted characters
    return n - maximum
 
# Driver Code
if __name__ == "__main__":
 
    S = "0110100110"
    minimum = findmin(S)
    print(minimum)
 
# This code is contributed by akhilsaini


C#
// C# Program to implement
// the above approach
using System;
class GFG{
 
// Function to find minimum
// characters to be removed
// such that no subsequence
// of the form "0101" exists
// in the string
static int findmin(string s)
{
  int n = s.Length;
  int i, j, maximum = 0;
 
  // Stores the partial sums
  int[] incr = new int[n + 1];
 
  for (i = 0; i < n; i++)
  {
    // Calculate partial sums
    incr[i + 1] = incr[i];
 
    if (s[i] == '0')
    {
      incr[i + 1]++;
    }
  }
 
  for (i = 0; i < n; i++)
  {
    for (j = i + 1; j < n; j++)
    {
      // Setting endpoints and
      // deleting characters indices.
      maximum = Math.Max(maximum, incr[i] +
                         j - i + 1 -
                        (incr[j + 1] - incr[i]) +
                         incr[n] - incr[j + 1]);
    }
  }
 
  // Return count of
  // deleted characters
  return n - maximum;
}
   
// Driver Code
public static void Main()
{
  string S = "0110100110";
  int minimum = findmin(S);
  Console.WriteLine(minimum);
}
}
 
// This code is contributed by akhilsaini


输出:
3


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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live