📌  相关文章
📜  将所有1保持在二进制字符串所需的最小翻转

📅  最后修改于: 2021-05-25 06:44:24             🧑  作者: Mango

给定二进制字符串str ,任务是找到在给定的二进制字符串中将所有1保持在一起所需的最小翻转次数,即,字符串的1之间不得有任何0。
例子:

方法:为解决上述问题,我们将实现动态编程方法,其中将具有以下状态:

  • 第一个状态是dp [i] [0] ,它表示使所有零达到第i位所需的翻转次数。
  • 第二状态dp [i] [1]表示制造当前位1以便满足问题中给出的条件所需的翻转次数。

因此,对于所有i值,所需的答案将是使当前位为1的最小翻转+使当前位为0的所有翻转的最小翻转。但是,如果给定字符串中的所有位均为0,则无需更改任何内容,因此我们可以检查答案和使字符串全为零所需的翻转次数之间的最小值。因此,我们可以通过遍历字符串中的所有字符来计算答案,其中

下面是上述方法的实现:

C++
//cpp implementation for Minimum number of
//flips required in a binary string such
//that all the 1’s are together
#include 
using namespace std;
int minFlip(string a)
{
     //Length of the binary string
    int n = a.size();
 
    vector> dp(n + 1,vector(2, 0));
 
    //Initial state of the dp
    //dp[0][0] will be 1 if the current
    //bit is 1 and we have to flip it
    dp[0][0] = (a[0] == '1');
 
    //Initial state of the dp
    //dp[0][1] will be 1 if the current
    //bit is 0 and we have to flip it
    dp[0][1] = (a[0] == '0');
 
 
    for (int i = 1; i < n; i++)
    {
        //dp[i][0] = Flips required to
        //make all previous bits zero
        //+ Flip required to make current bit zero
        dp[i][0] = dp[i - 1][0] + (a[i] == '1');
 
 
        //dp[i][1] = mimimum flips required
        //to make all previous states 0 or make
        //previous states 1 satisfying the condition
        dp[i][1] = min(dp[i - 1][0],
                       dp[i - 1][1]) + (a[i] == '0');
 
    }
    int answer = INT_MAX;
    for (int i=0;i


Java
// Java implementation for
// Minimum number of flips
// required in a binary string
// such that all the 1’s are together
import java.io.*;
import java.util.*;
class GFG{
     
static int minFlip(String a)
{
  // Length of the binary string
  int n = a.length();
 
  int dp[][] = new int[n + 1][2];
 
  // Initial state of the dp
  // dp[0][0] will be 1 if
  // the current bit is 1
  // and we have to flip it
  if(a.charAt(0) == '1')
  {
    dp[0][0] = 1 ;
  }
  else dp[0][0] = 0;
 
  // Initial state of the dp
  // dp[0][1] will be 1 if
  // the current bit is 0
  // and we have to flip it
  if(a.charAt(0) == '0')
    dp[0][1] = 1;
  else dp[0][1] = 0;
 
  for (int i = 1; i < n; i++)
  {
    // dp[i][0] = Flips required to
    // make all previous bits zero
    // Flip required to make current
    // bit zero
    if(a.charAt(i) == '1')
    {
      dp[i][0] = dp[i - 1][0] + 1;
    }
    else dp[i][0] = dp[i - 1][0];
 
    // dp[i][1] = mimimum flips
    // required to make all previous
    // states 0 or make previous states
    // 1 satisfying the condition
    if(a.charAt(i) == '0')
    {
      dp[i][1] = Math.min(dp[i - 1][0],
                          dp[i - 1][1]) + 1;
    }
    else dp[i][1] = Math.min(dp[i - 1][0],
                             dp[i - 1][1]);
  }
 
  int answer = Integer.MAX_VALUE;
  for (int i = 0; i < n; i++)
  {
    answer = Math.min(answer, dp[i][1] +
                      dp[n - 1][0] -
                      dp[i][0]);
  }
 
  //Minimum of answer and flips
  //required to make all bits 0
  return Math.min(answer,
                  dp[n - 1][0]);
}
   
// Driver code
public static void main (String[] args)
{
  String s = "1100111000101";
  System.out.println(minFlip(s));
}
}
 
// This code is contributed by satvikshrivas26


Python3
# Python implementation for Minimum number of
# flips required in a binary string such
# that all the 1’s are together
 
def minFlip(a):
     
     # Length of the binary string
    n = len(a)
     
    dp =[[0, 0] for i in range(n)]
     
    # Initial state of the dp
    # dp[0][0] will be 1 if the current
    # bit is 1 and we have to flip it
    dp[0][0]= int(a[0]=='1')
     
    # Initial state of the dp
    # dp[0][1] will be 1 if the current
    # bit is 0 and we have to flip it
    dp[0][1]= int(a[0]=='0')
     
 
    for i in range(1, n):
         
         
        # dp[i][0] = Flips required to
        # make all previous bits zero
        # + Flip required to make current bit zero
        dp[i][0]= dp[i-1][0]+int(a[i]=='1')
         
         
        # dp[i][1] = mimimum flips required
        # to make all previous states 0 or make
        # previous states 1 satisfying the condition
        dp[i][1]= min(dp[i-1])+int(a[i]=='0')
         
     
 
    answer = 10**18
     
    for i in range(n):
        answer = min(answer,
                     dp[i][1]+dp[n-1][0]-dp[i][0])
     
    # Minimum of answer and flips
    # required to make all bits 0
    return min(answer, dp[n-1][0])
     
 
# Driver code
s = "1100111000101"
 
print(minFlip(s))


C#
// C# implementation for
// Minimum number of
// flips required in
// a binary string such
// that all the 1’s are together
using System;
class GFG{
  
static int minFlip(string a)
{
  //Length of the binary string
  int n = a.Length;
 
  int [,]dp=new int[n + 1, 2];
 
  //Initial state of the dp
  //dp[0][0] will be 1 if the current
  //bit is 1 and we have to flip it
  dp[0, 0] = (a[0] == '1' ? 1 : 0);
 
  //Initial state of the dp
  //dp[0][1] will be 1 if the current
  //bit is 0 and we have to flip it
  dp[0, 1] = (a[0] == '0' ? 1 : 0);
 
  for (int i = 1; i < n; i++)
  {
    //dp[i][0] = Flips required to
    //make all previous bits zero
    //+ Flip required to make current bit zero
    dp[i, 0] = dp[i - 1, 0] +
                 (a[i] == '1' ? 1 : 0);
 
    //dp[i][1] = mimimum flips required
    //to make all previous states 0 or make
    //previous states 1 satisfying the condition
    dp[i, 1] = Math.Min(dp[i - 1, 0],
                        dp[i - 1, 1]) +
                       (a[i] == '0' ? 1 : 0);
 
  }
  int answer = int.MaxValue;
   
  for (int i = 0; i < n; i++)
  {
    answer = Math.Min(answer, dp[i, 1] +
                      dp[n - 1, 0] - dp[i, 0]);
  }
 
  //Minimum of answer and flips
  //required to make all bits 0
  return Math.Min(answer, dp[n - 1, 0]);
}
  
// Driver code
public static void Main(string[] args)
{
  string s = "1100111000101";
  Console.Write(minFlip(s));
}
}
 
// This code is contributed by Rutvik_56


输出:
4