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

📅  最后修改于: 2021-04-21 23:40:09             🧑  作者: 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)